Leetcode 27. Remove Element

2019-05-25 22:53:41 浏览数 (1)

1. Description

2. Solution

  • Version 1
代码语言:javascript复制
class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int count = 0;
        for(int i = 0; i < nums.size(); i  ) {
            if(nums[i] == val) {
                count  ;
                continue;
            }
            nums[i - count] = nums[i];
        }
        return nums.size() - count;
    }
};
  • Version 2
代码语言:javascript复制
class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int count = 0;
        for(int i = 0; i < nums.size(); i  ) {
            if(nums[i] != val) {
                nums[count] = nums[i];
                count  ;
            }

        }
        return count;
    }
};

0 人点赞