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

    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.

    Example 2:

    Given nums = [0,0,1,1,1,1,2,3,3],
    
    Your function should return length = 7, with the first seven elements of nums being modified to 0, 0, 1, 1, 2, 3 and 3 respectively.
    
    It doesn't matter what values are set beyond the returned length.

    Clarification:

    Confused why the returned value is an integer but your answer is an array?

    Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.

    Internally you can think of this:

    // nums is passed in by reference. (i.e., without making a copy)
    int len = removeDuplicates(nums);
    
    // any modification to nums in your function would be known by the caller.
    // using the length returned by your function, it prints the first len elements.
    for (int i = 0; i < len; i++) {
        print(nums[i]);
    }

    删除排序数组中的重复项 II。

    给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度。不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。

    思路跟版本一差不多,但是需要隔开两个位置。创建一个p指针,指向第一个无效的位置。比如第一个例子,p一开始指向第三个1的位置,因为这个位置上的数字一定是无效的,需要被替换。因为题目要求可以有重复两次的元素所以头两个元素不需要改动。然后每次去跟p - 2上数字比较,看是否重复,没有重复就放入数组同时p++。

    时间O(n)

    空间O(1)

    Java实现

     1 class Solution {
     2     public int removeDuplicates(int[] nums) {
     3         // corner case
     4         if (nums.length <= 2) {
     5             return nums.length;
     6         }
     7 
     8         // normal case
     9         int p = 2;
    10         for (int i = 2; i < nums.length; i++) {
    11             if (nums[i] != nums[p - 2]) {
    12                 nums[p] = nums[i];
    13                 p++;
    14             }
    15         }
    16         return p;
    17     }
    18 }

    JavaScript实现

     1 /**
     2  * @param {number[]} nums
     3  * @return {number}
     4  */
     5 var removeDuplicates = function(nums) {
     6     // corner case
     7     if (nums.length <= 2) return nums.length;
     8 
     9     // normal case
    10     let res = 2;
    11     for (let i = 2; i < nums.length; i++) {
    12         if (nums[i] !== nums[res - 2]) {
    13             nums[res] = nums[i];
    14             res++;
    15         }
    16     }
    17     return res;
    18 };

    LeetCode 题目总结

  • 相关阅读:
    Java编程语言学习01-Java语言概述
    Java复习面试指南-06为什么要进行数据类型转换?什么情况下会进行自动类型转换?
    Java复习面试指南-05简单说一下Java当中的char字符类型?
    Java复习面试指南-04Java语言支持的8种基本数据类型是什么?占用的空间是多少?
    Java复习面试指南03-说一下Java当中标识符与关键字的区别?
    Linq LeftJoin 取不同和想同的对像
    vue父组件异步传递prop到子组件echarts画图问题踩坑总结
    linux下使用openssl生成https的crt和key证书
    css hover延时 解决快速划入划出
    记录时间操作
  • 原文地址:https://www.cnblogs.com/cnoodle/p/12749299.html
Copyright © 2011-2022 走看看