首页 > 其他 > 详细

[LeetCode]27 Remove Element

时间:2015-01-02 16:09:29      阅读:243      评论:0      收藏:0      [点我收藏+]

https://oj.leetcode.com/problems/remove-element/

http://fisherlei.blogspot.com/2012/12/leetcode-remove-element.html

public class Solution {
    public int removeElement(int[] A, int elem) {
        
        // The order can be changed.
        // Use 2 pointers.
        // One to iterate the array,
        // One to copy the last elements back to array.

        if (A == null || A.length == 0)
            return 0;
        
        int start = 0;
        int end = A.length - 1;
        while (start <= end)
        {
            int v = A[start];
            if (v == elem)
            {
                // Copy A[end] to A[start]
                A[start] = A[end];
                end --;
            }
            else
            {
                start ++;
            }
        }
        return end + 1;
    }
}


[LeetCode]27 Remove Element

原文:http://7371901.blog.51cto.com/7361901/1598427

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