Leetcode 线性表 Remove Element

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie

Remove Element

Total Accepted: 13840 Total
Submissions: 42676

Given an array and a value, remove all instances of that value in place and return the new length.

The order of elements can be changed. It doesn‘t matter what you leave beyond the new length.

题意:移除数组中出现的给定元素,并返回移除元素数组长度。要求在数组上操作

思路:遍历数组,将除给定元素外的其他元素赋值给“新数组”,不过这个“新数组”还是在原来的“旧数组”的内存空间

因为遍历旧数组的下标总大于等于新数组的下标,所以赋值并不会影响到还没有遍历到的元素

复杂度:时间O(n), 空间O(1)

class Solution {
public:
    int removeElement(int A[], int n, int elem){
    	int i = 0;
    	for(int j = 0; j < n; j++){
    		if(A[j] != elem) A[i++] = A[j];
    	}
    	return i;
    }
};

Leetcode 线性表 Remove Element,布布扣,bubuko.com

时间: 2024-10-12 09:23:13

Leetcode 线性表 Remove Element的相关文章

Leetcode 线性表 Remove Duplicates from Sorted Array

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted Array Total Accepted: 14789 Total Submissions: 46601 Given a sorted array, remove the duplicates in place such that each element appear only once and return the new l

Leetcode 线性表 Remove Duplicates from Sorted List

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted List Total Accepted: 14961 Total Submissions: 43772 Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1->1

Leetcode 线性表 Remove Duplicates from Sorted Array II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted Array II Total Accepted: 10649 Total Submissions: 35325 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorte

Leetcode 线性表 Remove Nth Node From End of List

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Nth Node From End of List Total Accepted: 12160 Total Submissions: 41280 Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1

Leetcode 线性表 Remove Duplicates from Sorted List II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted List II Total Accepted: 10702 Total Submissions: 43714 Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from th

Leetcode 线性表 Swap Nodes in Pairs

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Total Submissions: 39302 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the

Leetcode 线性表 Linked List Cycle

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Linked List Cycle Total Accepted: 17041 Total Submissions: 48975 Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 题意:判断一个链表中是否有环 思路:快慢

Leetcode 线性表 数 Add Two Numbers

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Add Two Numbers Total Accepted: 13127 Total Submissions: 58280 You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes

Leetcode 线性表 Two Sum

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Two Sum Total Accepted: 19206 Total Submissions: 103959 Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of