其他分享
首页 > 其他分享> > LeetCode 0080 Remove Duplicates from Sorted Array II

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)

标签:nums,Duplicates,复杂度,Remove,II,int,duplicates,new,array
来源: https://www.cnblogs.com/junstat/p/16193197.html