zoukankan      html  css  js  c++  java
  • 80. Remove Duplicates from Sorted Array II

    package LeetCode_80
    
    /**
     * 80. Remove Duplicates from Sorted Array II
     * https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/description/
     *
     * Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.
    Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
    
    Example 1:
    Given nums = [1,1,1,2,2,3],
    Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively.
    It doesn't matter what you leave beyond the returned length.
     * */
    class Solution {
        /*
        solution: for loop to check i and each two number, Time complexity:O(n), Space complexity:O(1)
        * */
        fun removeDuplicates(nums: IntArray): Int {
            var index = 0
            for (num in nums) {
                //if current num large than prev two numbers, mean current number is new one after two duplicates
                if (index < 2 || num > nums[index - 2]){
                    nums[index++] = num
                }
            }
            return index
        }
    }
  • 相关阅读:
    NOI2018 退役记
    APIO2018 被屠记
    CTSC2018 被屠记
    SNOI2018 退役记
    O(1) long long a*b%p
    prufer编码
    杜教筛
    GCC卡常
    NOIP2017滚粗记
    UVA 10763 Foreign Exchange
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13698245.html
Copyright © 2011-2022 走看看