one-pass Java Solution


  • 0
    K
    public class Solution {
        public int removeDuplicates(int[] nums) {
            if (nums.length == 1)
                return 1;
            int length = 0;
            for (int i = 0, j = 0; j < nums.length; i++) {
                while (nums[i] == nums[j]) {
                    j++;
                    if (j == nums.length) {
                        break;
                    }
                }
                if (j != nums.length) {
                    nums[i + 1] = nums[j];
                }
                length++;
            }
            return length;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.