码迷,mamicode.com
首页 > 其他好文 > 详细

Remove Element ——结题报告

时间:2015-05-12 15:55:06      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:leetcode   去重   vector   


     【题目】

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<int>& nums, int val) {
        if(nums.size() < 1)
            return 0;
        int len = nums.size();
        
        for(int i = 0 ; i < len; i++)
        {
            if(nums[i] != val)
                continue;
            else
            {
                len--;
                nums.erase(nums.begin() + i);
                i--;
            }
        }
        return len;
    }
};



Remove Element ——结题报告

标签:leetcode   去重   vector   

原文地址:http://blog.csdn.net/puqutogether/article/details/45668437

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!