Description
Given an integer array nums sorted in non-decreasing order, remove the
duplicates in-place such that each unique element appears only
once. The relative order of the elements should be kept the
same. Then return the number of unique elements in nums.
Consider the number of unique elements of nums to be k, to get accepted, you need
to do the following things:
- Change the array
numssuch that the firstkelements ofnumscontain the unique elements in the order they were present innumsinitially. The remaining elements ofnumsare not important as well as the size ofnums. - Return
k.
Custom Judge:
The judge will test your solution with the following code:
|
|
If all assertions pass, then your solution will be accepted.
Example 1:
|
|
Example 2:
|
|
Constraints:
1 <= nums.length <= 3 * 104-100 <= nums[i] <= 100numsis sorted in non-decreasing order.
풀이
주어진 nums는 non-decresing order로 정렬이 되어있기 떄문에 idx =1부터 시작하여 i와 i-1이 다른 숫자라면 nums[idx]에 nums[i]를 저장하여 풀이했다.
코드
|
|