原地删除重复元素,并且返回删除后的新长度

    1. class Solution {
    2. public int removeDuplicates(int[] nums) {
    3. if(nums == null || nums.length == 0) {
    4. return 0;
    5. }
    6. if(nums.length == 1) {
    7. return 1;
    8. }
    9. int left = 0;
    10. for(int right = 1; right < nums.length; right++) {
    11. if(nums[left] != nums[right]) {
    12. nums[++left] = nums[right];
    13. }
    14. }
    15. return left + 1;
    16. }
    17. }