← 返回 meta 的题目列表Remove Duplicates from Sorted Array
类型:online_judge
Problem
Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once, and return the new length k.
You must ensure:
Use O(1) extra space.
The modification is done within the input array nums.
After returning k, the first k elements of nums contain the unique elements in the same relative order.
Input Format
One line of integers representing array nums (space-separated).
Output Format
Output an integer k, the length after removing duplicates.
Constraints
0 <= len(nums) <= 3 * 10^4
-10^4 <= nums[i] <= 10^4
nums is sorted in non-decreasing order
Examples
Example 1
Input:
1 1 2
Output:
2
Explanation: The first 2 elements become [1, 2].
Example 2
Input:
0 0 1 1 1 2 2 3 3 4
Output:
5
Explanation: The first 5 elements become [0, 1, 2, 3, 4].
Example
Input
1 1 2
Output
2