leetcode.com/problems/remove-duplicates-from-sorted-array/
Given a sorted array nums, remove the duplicates in-place such that each element appears only once and returns the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
문제를 보면 길이에 따라서 output int를 이용하여 nums array를 읽는 것을 확인 할 수 있다.
class Solution {
public int removeDuplicates(int[] nums) {
if (nums.length == 0) {
return 0;
}
int numberCount = 1;
for (int index = 0; index < nums.length - 1; index++) {
if (nums[index] != nums[index + 1]) {
numberCount++;
}
}
return numberCount;
}
}
'개발자 > algorithm' 카테고리의 다른 글
LeetCode, Minimum Number of Steps to Make Two Strings Anagram (Java) (0) | 2020.12.29 |
---|---|
LeetCode, Goal Parser Interpretation (Java) (0) | 2020.12.29 |
LeetCode, Merge Two Sorted Lists (Java) (0) | 2020.12.22 |
LeetCode, Two Sum (Java) (0) | 2020.12.22 |
백준 19238번 : 스타트 택시 (c++) (0) | 2020.10.01 |