[LeetCode]Remove Element

public class Solution {
    public int removeElement(int[] nums, int val) {
        if (nums.length == 0) {
            return 0;
        }
        int left = 0;
        int right = nums.length - 1;
        while (left < right) {
            if (nums[left] != val || nums[right] == val) {
                if (nums[left] != val) {
                    left ++;
                }
                if (nums[right] == val) {
                    right --;
                }
            } else {
                int tmp = nums[left];
                nums[left] = nums[right];
                nums[right] = tmp;
            }
        }
        return nums[left] == val ? left : left + 1;
    }
}

 

posted @ 2015-12-03 03:04  Weizheng_Love_Coding  阅读(120)  评论(0编辑  收藏  举报