【Leetcode】之Remove Element

原创 2015年11月20日 15:15:18

一.问题描述

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.

二.我的解题思路

这道题目有点像上一道题,都比较简单。我的做法是每次都记录之前一个非value数的下标pri,当遇到新的非value数的时候,直接放在pri后面即可。测试通过的程序如下:

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int cnt=0;int len=nums.size();
        int st=0;int pri=-1;
        if (len==0) return 0;
        if(len==1&&nums[0]==val) return 0;
        if(len==1&&nums[0]!=val) return 1;
        if(nums[0]==val) pri=-1;
        else {pri=0;cnt=1;}
        for(int i=1;i<len;i++){
            if(nums[i]!=val){
                nums[pri+1]=nums[i];
                pri++;
                cnt++;
            }


        }
        return cnt;
    }
};

相关文章推荐

Leetcode_27_Remove Element

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41577997 Remove Element Given ...

LeetCode_27---Remove Element

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

LeetCode[Array]: Remove Element

Given an array and a value, remove all instances of that value in place and return the new length. T...
  • chfe007
  • chfe007
  • 2014年10月31日 00:38
  • 664

LeetCode 之 Remove Element

原题: Given an array and a value, remove all instances of that value in place and return the new leng...

LeetCode | Remove Element(删除指定元素)

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

LeetCode笔记:27. Remove Element

固定内存下移除数组中特定的数字

[LeetCode][Java] Remove Element

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

Leetcode - Array - 27. Remove Element(第一道题)

1.Problem description Given an array and a value, remove all instances of that value in place and re...

Leetcode - Remove Element

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

关于LeetCode中Remove Element一题的理解

题目如下: Given an array and a value, remove all instances of that value in place and return the ne...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【Leetcode】之Remove Element
举报原因:
原因补充:

(最多只允许输入30个字)