LeetCode 0080 Remove Duplicates from Sorted Array II
1. 题目描述


2. Solution 1
1、思路分析
Question wants us to return the length of new array after removing duplicates and that we don't care about what we leave beyond new length , hence we can use i to keep track of the position and update the array.
2、代码实现
package Q0099.Q0080RemoveDuplicatesFromSortedArrayII; public class Solution { /* Q026 的续集,Remove Duplicates from Sorted Array II (allow duplicates up to 2): */ public int removeDuplicates(int[] nums) { int i = 0; for (int n : nums) { if (i < 2 || n > nums[i - 2]) nums[i++] = n; } return i; } } 3、复杂度分析
时间复杂度: O(n)
空间复杂度: O(1)