Remove Duplicates from Sorted Array
-
[leetcode] 26. Remove Duplicates from Sorted Array알고리즘 2021. 11. 16. 22:29
Description 내림차순 정수 배열이 주어졌을때, 해당 배열에서 중복을 모두 제거 한 후 배열의 길이를 출력하는 문제 Input: arr = [1, 1, 2] Output: [1, 2, _] Input: arr = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4] Output: [0, 1, 2, 3, 4, _, _, _, _, _] Success 두개의 변수를 사용하여 각각 다른 인덱스를 바라보게 한다. 두 변수가 가르키고 있는 인덱스의 값이 다르면 특정 변수가 가르키고 있는 인덱스에 값을 할당하여 중복이 제거될 수 있도록 한다. class Solution { public int removeDuplicates(int[] nums) { int j= 0; for(int i=1; i
-
[leetcode] 26. Remove Duplicates from Sorted Array카테고리 없음 2021. 11. 10. 08:06
Description 내림차순으로 정렬된 배열이 주어졌을때 배열 안에 중복된 값들을 제거하는 문제. 단, 새로운 배열 추가 없이 기존 배열에서 중복이 제거되어야 한다. Input: nums = [1,1,2] Output: 2, nums = [1,2,_] Input: nums = [0,0,1,1,1,2,2,3,3,4] Output: 5, nums = [0,1,2,3,4,_,_,_,_,_] Success cnt 라는 변수를 하나 두고 for문이 적용된 i 변수의 인덱스의 값과 cnt 변수의 인덱스 값을 비교하여 다른 경우에만 앞에서부터 새롭게 할당시킨다. class Solution { public int removeDuplicates(int[] nums) { if (nums == null) return -1..