首页 > 其他 > 详细

leetcode removeElement

时间:2015-10-23 18:39:43      阅读:66      评论:0      收藏:0      [点我收藏+]

27.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) {
        unsigned int tag = 0;
        auto len = nums.size();
        for (int i = 0; i < len; ++i){
            if (nums[i] != val && tag != 0)nums[i - tag] = nums[i];
            if (nums[i]== val)++tag;
        }
        return len-tag;
    }
};

我用的方法是将!=指定的数往前移,最后返回容器中不等于指定的数的数目

leetcode removeElement

原文:http://www.cnblogs.com/csudanli/p/4905132.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!