本文共 1399 字,大约阅读时间需要 4 分钟。
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.
class Solution {public: int removeElement(vector & nums, int val) { int n = nums.size(); int i = 0; while( i < n ) { if( nums[i] == val ) { swap(nums[i], nums[n-1]); n--; } else { i++; } } return n; }};
int removeElement(vector & nums, int val){ vector ::iterator itr = nums.begin(); while (itr != nums.end()) { if (*itr == val) itr = nums.erase(itr); else ++itr; } return nums.size();}
int removeElement(int A[], int n, int elem) { int begin=0; for(int i=0;i
class Solution:# @param A a list of integers# @param elem an integer, value need to be removed# @return an integerdef removeElement(self, A, elem): i = 0 for j in range(len(A)): if A[j] != elem: A[i] = A[j] i += 1 return i
def removeElement(self, nums, val): nums[:] = [x for x in nums if x!=val] return len(nums)
转载地址:http://bdfkl.baihongyu.com/