Remove Element Leetcode

Given an array and a value, remove all instances of that value in place and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

Example:
Given input array nums = [3,2,2,3]val = 3

Your function should return length = 2, with the first two elements of nums being 2.

Hint:

  1. Try two pointers.
  2. Did you use the property of "the order of elements can be changed"?
  3. What happens when the elements to remove are rare?

 

Subscribe to see which companies asked this question

这道题我真的是。。。

用了传统的双指针,但是如果条件是left < right怎么都不行,换成left <= right就对了。

因为left < right没有考虑[3, 3, 3, 3], 3的情况,这个时候length还是1。所以有没有等号要看一下极端情况。

另外需要注意swap的时候其实也要检查left<=right

public class Solution {
    public int removeElement(int[] nums, int val) {
        if (nums == null) {
            return 0;
        }
        int left = 0, right = nums.length - 1;
        int l = nums.length;
        while (left <= right) {
            while (left <= right && nums[left] != val) {
                left++;
            }
            while (left <= right && nums[right] == val) {
                right--;
                l--;
            }
            if (left <= right) {
                nums[left] = nums[right];
                left++;
                right--;
                l--;
            }
        }
        return l;
    }
}

反正做出来了,就是代码很长。。。看完人家的top solution,觉得自己的方法真是笨啊。。

public class Solution {
    public int removeElement(int[] nums, int val) {
        if (nums == null) {
            return 0;
        }
        int n = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != val) {
                nums[n] = nums[i];
                n++;
            }
        }
        return n;
    }
}

时隔这么久又来做了一次,果然还是用的笨办法。。。= =

也许代码质量好一点了吧。。。自我安慰一下。。。

public class Solution {
    public int removeElement(int[] nums, int val) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int end = nums.length - 1;
        int len = 0;
        for (int i = 0; i <= end; i++) {
            while (end >= 0 && nums[end] == val) {
                end--;
            }
            if (i > end) {
                return len;
            }
            if (nums[i] == val) {
                swap(nums, i, end);
                end--;
            }
            len++;
        }
        return len;
    }
    private void swap(int[] nums, int a, int b) {
        int temp = nums[a];
        nums[a] = nums[b];
        nums[b] = temp;
    }
}

 顺便用c++写一下:

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int len = 0;
        for (int i = 0; i < nums.size(); i++) {
            if (nums[i] != val) {
                nums[len] = nums[i];
                len++;
            }
        }
        return len;
    }
};

 

posted @ 2017-01-19 10:48  璨璨要好好学习  阅读(135)  评论(0编辑  收藏  举报