leetcode--27. Remove Element

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

Do not allocate extra space for another array, you must do this in place with constant memory.

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

Example:
Given input array nums = [3,2,2,3]val = 3

Your function should return length = 2, with the first two elements of nums being 2

这个和上一题几乎一个意思...脑残的很...http://blog.csdn.net/mmshixing/article/details/52152094

就当熟悉一下STL基本操作吧

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        if(nums.size() == 0)
            return 0;
        for(vector<int>::iterator ite = nums.begin(); ite != nums.end(); ){
            if(*ite == val ){
                ite = nums.erase(ite);
            }
            else
                ++ite;
        }
        return nums.size();
    }
};


猜你喜欢

转载自blog.csdn.net/mmshixing/article/details/52152329