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

[LeetCode]27 Remove Element

时间:2015-01-02 16:10:11      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:leetcode

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

标签:leetcode

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

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