首页 > 其他分享 >【刷题笔记】80. Remove Duplicates from Sorted Array II

【刷题笔记】80. Remove Duplicates from Sorted Array II

时间:2023-10-12 13:00:53浏览次数:30  
标签:slow nums Duplicates Remove II length 数组 array 指针

题目

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 = removeElement(nums, val);

// 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]);
}

题目大意

给定一个有序数组 nums,对数组中的元素进行去重,使得原数组中的每个元素最多暴露 2 个。最后返回去重以后数组的长度值。

解题思路

  • 问题提示有序数组,一般最容易想到使用双指针的解法,双指针的关键点:移动两个指针的条件。
  • 在该题中移动的条件:快指针从头遍历数组,慢指针指向修改后的数组的末端,当慢指针指向倒数第二个数与快指针指向的数不相等时,才移动慢指针,同时赋值慢指针。
  • 处理边界条件:当数组小于两个元素时,不做处理。

参考代码

package leetcode

func removeDuplicates(nums []int) int {
	slow := 0
	for fast, v := range nums {
		if fast < 2 || nums[slow-2] != v {
			nums[slow] = v
			slow++
		}
	}
	return slow
}

标签:slow,nums,Duplicates,Remove,II,length,数组,array,指针
From: https://blog.51cto.com/u_16110811/7826093

相关文章

  • C#中如何获得ASCII码的字母?
        ASCII码是计算机的基础,有时编程过程中也要遇到,这里重点介绍0-127之间的ASCII码表。   0~31及127(共33个)是控制字符和通信专用字符。控制字符,如LF(换行)、CR(回车)、FF(换页)、DEL(删除)、BS(退格)、BEL(响铃)等。通信专用字符,如SOH(文头)、EOT(文尾)、ACK(确认......
  • ASCII 码对照表
    ASCII(10进制)16进制控制字符ACSII(10进制)16进制控制字符ACSII(10进制)16进制字符ASCII(10进制)16进制字符00NUT3220NULL6440@9660`11SOH3321!6541A9761a22STX3422”6642B9862b33ETX3523#6743C9963c44EOT......
  • 【题解】Fibonacci-ish II
    传送门题目分析根据题目范围\(n\le30000\)并且此题可以离线维护这个很恶心的东西,所以我们考虑莫队。由于要求访问到任意一个区间都要求知道它有序之后的序列,所以这个东西可以用权值线段树维护。因此,此题正解是莫队+权值线段树。我们分类讨论一下加上一个数,删除一个数对答案......
  • mysqldump 导出来的文件,使用 source还原时报错“ASCII '\0' appeared in the stateme
    导出语句:mysqldump-uroot-pword--databasesdb1--tablestable1>./sqldumps/archive-table1-`date+"%Y%m%d_%H%M%S"`.sql导出后,使用source还原报错:ASCII'\0'appearedinthestatement,butthisisnotallowedunlessoption我开始以为是我导出的编码格式有问题,......
  • Go - Remove values from a slice
    Totakeoutthefirstelementoftheslice:numbers:=[]int{3,14,159,26,53,58}numbers=numbers[1:]//removeelement0To takeoutthelastelementoftheslice:numbers:=[]int{3,14,159,2......
  • LeetCode——95. 不同的二叉搜索树 II
    本次博客,我将记录leetcode95,不同的二叉搜索树95.不同的二叉搜索树II本题要求我们从1~n构造不同的二叉搜索树因为好久不碰数据结构了,导致对二叉搜索树的概念十分模糊以下是一些概念:二叉搜索树(BST,BinarySearchTree),也称二叉排序树或二叉查找树。性质如下:1.非空左子树的所......
  • 2023 ICPC 网络预选赛补题 II
    2023ICPC网络预选赛II赛时AC题目M. DirtyWork点击查看代码#include<bits/stdc++.h>#definelddoubleusingnamespacestd;constintmaxn=1e6+5;inta[maxn],b[maxn];ldp[maxn],c[maxn];intt,n;boolcmp(lda,ldb){ returna<b;}intmain(){ scanf(&quo......
  • 数字信号处理-IIR滤波器
    1.IIR滤波器的设计步骤首先设计满足技术指标的模拟滤波器将模拟滤波器转换为数字滤波器2.如何设计模拟滤波器将任意的模拟滤波器指标转换为低通的模拟滤波器指标设计好低通滤波器(关键步骤)通过变换,将低通滤波器转换成任意的模拟滤波器低通模拟滤波器有三个模板:BW、CB、C......
  • 关于 VMware 虚拟机中的 SVGA II 虚拟设备
    VMwareSVGAII是VMware虚拟机中用于图形显示的虚拟显卡设备的一种。它是一种虚拟设备,专门为在虚拟机环境中提供图形支持而设计的。VMwareSVGAII虚拟设备的作用是模拟物理计算机上的图形适配器,允许虚拟机实例在虚拟化的环境中进行图形处理和显示。在本文中,我将详细解释VMwar......
  • 字符串小记 II:字符串自动机
    OI中的自动机指的是“有限状态自动机”,它是对一串信号进行处理的数学模型,一般由以下三部分构成:字符集(\(\Sigma\)),能够输入进自动机的字符集合。状态集合(\(Q\)),相当于有向图中的节点。转移函数(\(\delta\)),相当于有向图中的边。我们通过输入的信息在这个有向图中转移,而这个有......