首页 > 其他 > 详细

LeetCode_27

时间:2019-03-09 17:39:42      阅读:173      评论:0      收藏:0      [点我收藏+]

题目描述:

Given an array nums and a value val, remove all instances of that value in-placeand return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

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

思路:

定义区间 [0,k)范围为无val的数列,遍历nums

代码如下:

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        if(nums.size() == 0) return 0;
        int k = 0;
        for(int i = 0; i < nums.size(); ++i)
        {
            if(nums[i] != val)
                nums[k++] = nums[i];
        }
        return k;
    }
};

 

LeetCode_27

原文:https://www.cnblogs.com/harchar/p/10502029.html

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