LeetCode-remove-element

原创 2016年08月30日 19:40:51

题目描述


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.


这个题目还要求将数组变为最后的样子,光计算出个数是不行的!

对于这种数组需要以为的题目,index!index!index!

public class Solution {
    public int removeElement(int[] A, int elem) {
        int index = 0;
        for(int i=0; i<A.length; i++) {
            if(A[i] != elem){
                A[index++] = A[i];
            }
        }
        
        return index;
    }
}

用index++记录最终留下元素的位置!!!切记!切记!

Remove Element 数组中删除给定元素 @ LeetCode

/** * * Remove Duplicates from Sorted Array * * Given a sorted array, remove the duplicates i...

leetcode 27. Remove Element

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

LeetCode 之 Remove Element — C/C++实现

Remove Element Given an array and a value, remove all instances of that value in place and return...

【leetcode c++】27 Remove Element

Remove Element Given an array and a value, remove allinstances of that value in place and return th...
  • hqq39
  • hqq39
  • 2015年06月22日 00:48
  • 370

[leetcode]Remove Element

Remove Element 题意:将一个list中某数删除,其他元素的顺序可以改变 解法:          O(n):从左到右遍历list,遇到elem,直接不管。遇到非elem,把它放到前...
  • eiwoqu
  • eiwoqu
  • 2014年05月27日 16:03
  • 207

[leetcode]Remove Element(break、continue)

题目:Given an array and a value, remove all instance of that value in place and return the new length....
  • aj_nn
  • aj_nn
  • 2015年01月20日 13:41
  • 173

LeetCode027 Remove Element

详细见:leetcode.com/problems/remove-element/ Java Solution: github package leetcode; public class ...
  • zxwtry
  • zxwtry
  • 2017年04月01日 12:08
  • 93

leetcode 27 Remove Element

27. Remove Element Given an array and a value, remove all instances of that value in place and ret...

Remove Element -- leetcode

数组元素删除

LeetCode 27. Remove Element 题解 —— Java

题目链接:https://leetcode.com/problems/remove-element/#/description
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode-remove-element
举报原因:
原因补充:

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