<예시1>
Input: nums = [1,1,2]
Output: 2, nums = [1,2,_]
<예시2>
Input: nums = [0,0,1,1,1,2,2,3,3,4]
Output: 5, nums = [0,1,2,3,4,,,,,_]
public class Solution {
public int removeDuplicates(int[] nums) {
if(nums.length == 0)
return 0;
int[] dst = new int[nums.length];
dst[0] = nums[0];
int cnt = 1;
for(int i=1, j=1; i<nums.length; ++i)
{
if(nums[i] != nums[i-1])
{
dst[j] = nums[i];
j++;
cnt++;
}
}
for(int i=0; i < nums.length; ++i)
{
nums[i] = dst[i];
}
return cnt;
}
}
출처 : 뱀귤 블로그
class Solution {
public int removeDuplicates(int[] nums) {
int index = 1;
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] != nums[i + 1]) {
nums[index++] = nums[i + 1];
}
}
return index;
}
}