LeetCode/Easy

26. Remove Duplicates from Sorted Array

GenieLove! 2022. 3. 29. 21:31
728x90
반응형

Python

class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        remove_index = 1
        
        while True:
            if remove_index >= len(nums):
                break
            if nums[remove_index] == nums[remove_index - 1]:
                nums.pop(remove_index)
            else:
                remove_index += 1
        
        return remove_index + 1

Java

class Solution {
    public int removeDuplicates(int[] nums) {
        int count = 1;
        int insert_index = 1;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] != nums[insert_index - 1]) {
                nums[insert_index] = nums[i];
                insert_index++;
                count++;
            }
        }
        return count;
    }
}
728x90
반응형

'LeetCode > Easy' 카테고리의 다른 글

100. Same Tree  (0) 2022.04.02
27. Remove Element  (0) 2022.03.31
70. Climbing Stairs  (1) 2022.03.27
112. Path Sum  (0) 2022.03.25
182. Duplicate Emails  (0) 2022.03.24