Here's my solution, but I think it looks ugly, some advice?


  • 0
    T
    class Solution {
        public int removeDuplicates(int[] nums) {
            if(nums.length == 0){
                return 0;
            }
            int result = 1;
            int pre_num = nums[0];
            for(int i = 0; i < nums.length; i++){
                if(nums[i] != pre_num){
                    nums[result] = nums[i];
                    pre_num = nums[i];
                    result ++;
                }
            }
            return result;
        }
    }
    

Log in to reply
 

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