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

LeetCode--Remove Element

时间:2014-12-21 23:41:49      阅读:390      评论:0      收藏:0      [点我收藏+]

标签:

这个题目没有动手实践,只是想了个思路,结果一看讨论区的代码瞬间感觉,我想的太复杂了。ps:有点想不明白,既然是要移除元素,为何不留下一个不含删除元素的纯净数组。

题目:

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. 


讨论区解决办法:

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


LeetCode--Remove Element

标签:

原文地址:http://blog.csdn.net/wj512416359/article/details/42065737

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