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

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

时间:2023-08-20 13:01:01浏览次数:34  
标签:26 return nums Duplicates Remove int length len last

题目

Given a sorted array nums, remove the duplicates in-place such that each element appear only once 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,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.

It doesn't matter what you leave beyond the returned length.

Example 2:

Given nums = [0,0,1,1,1,2,2,3,3,4],

Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 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,对数组中的元素进行去重,使得原数组中的每个元素只有一个。最后返回去重以后数组的长度值。

解题思路

这道题和第 27 题很像。这道题和第 283 题,第 27 题基本一致,283 题是删除 0,27 题是删除指定元素,这一题是删除重复元素,实质是一样的。

这里数组的删除并不是真的删除,只是将删除的元素移动到数组后面的空间内,然后返回数组实际剩余的元素个数,OJ 最终判断题目的时候会读取数组剩余个数的元素进行输出。

参考代码

package leetcode

// 解法一
func removeDuplicates(nums []int) int {
	if len(nums) == 0 {
		return 0
	}
	last, finder := 0, 0
	for last < len(nums)-1 {
		for nums[finder] == nums[last] {
			finder++
			if finder == len(nums) {
				return last + 1
			}
		}
		nums[last+1] = nums[finder]
		last++
	}
	return last + 1
}

// 解法二
func removeDuplicates1(nums []int) int {
	if len(nums) == 0 {
		return 0
	}
	length := len(nums)
	lastNum := nums[length-1]
	i := 0
	for i = 0; i < length-1; i++ {
		if nums[i] == lastNum {
			break
		}
		if nums[i+1] == nums[i] {
			removeElement1(nums, i+1, nums[i])
			// fmt.Printf("此时 num = %v length = %v\n", nums, length)
		}
	}
	return i + 1
}

func removeElement1(nums []int, start, val int) int {
	if len(nums) == 0 {
		return 0
	}
	j := start
	for i := start; i < len(nums); i++ {
		if nums[i] != val {
			if i != j {
				nums[i], nums[j] = nums[j], nums[i]
				j++
			} else {
				j++
			}
		}
	}
	return j
}

标签:26,return,nums,Duplicates,Remove,int,length,len,last
From: https://blog.51cto.com/u_16110811/7160662

相关文章

  • LCR 026. 重排链表
    LCR026.重排链表/***Definitionforsingly-linkedlist.*publicclassListNode{*intval;*ListNodenext;*ListNode(){}*ListNode(intval){this.val=val;}*ListNode(intval,ListNodenext){this.val=val;this.next......
  • PostgreSQL技术大讲堂 - 第26讲:PG-FDW部署
     PostgreSQL从小白到专家,是从入门逐渐能力提升的一个系列教程,内容包括对PG基础的认知、包括安装使用、包括角色权限、包括维护管理、、等内容,希望对热爱PG、学习PG的同学们有帮助,欢迎持续关注CUUGPG技术大讲堂。 第26讲:PG-FDW部署 内容1:ForeignDataWrappers(FDW)简......
  • Mcuzone CM4_WiFi6 WLAN Pi扩展板,支持8265 AX200 AX210 MT7921 MT7961 RTL8822
    关键词:树莓派CM4  WLANPi  WLANPi英特尔8265AX200AX210MT7921MT7961RTL8822ScannerWiFi6Mcuzone野芯科技概述:WLANPi是一款用途很广泛的WiFi工具,可以测试网速,可以对WiFi信号进行频谱分析。WLANPi可以在很多硬件平台上运行,其中最典型的就是树莓派平台。......
  • zlmediakit源码学习(扩展支持转码H265/H264)
    在zlmediakit源码基础上继续探索扩展支持H265/H264的转码功能。参照上一篇帖子:https://www.cnblogs.com/feixiang-energy/p/17623567.html 作者已经封装好了基于ffmpeg实现的解码、编码、视频图像格式转换、音频重采样等接口,https://gitee.com/xia-chu/ZLMediaKit/blob/feature......
  • 国产麒麟系统KylinOS Server V10 SP2安装MySQL 8.0.26—源码编译安装
    一:操作系统环境检查1.1首先确认操作系统版本是KylinOSServerV10SP2麒麟操作系统KylinosServerV10SP2使用的安装介质是Kylin-Server-10-SP2-x86-Release-Build09-20210524.iso,执行以下命令查看版本:cat/etc/kylin-releasecat/proc/version 1.2检查系统是否......
  • 如何实现RTSP推送H.264、RTSP推送H.265(hevc)
    1.rtsp推送流程.主要分两部分:第一部分先发送信令;第二部分发送rtp包。信令流程:1.1先发送OPTIONS,OPTIONS比较常用,就不做详细说明了。1.2发送ANNOUNCE,发送ANNOUNCE主要是把要推送的音视频信息通过sdp格式传给服务器。关于sdp信息如何构造,对于h264请参考rfc6184.h265请参考r......
  • 26、华为eNSP模拟器
    eNSP-企业网络仿真平台,是华为模拟器仿真平台,图形化操作界面,主要对路由器、交换机、防火墙进行软件仿真实验,方便实验操作。链接地址:https://forum.huawei.com/enterprise/zh/thread/580934378039689216   亲测好用 ......
  • GitHub: remote:Support for password authentication was removed on August 13,2021
    使用gitpushoriginmaster向远程仓库推送时被告知:remote:SupportforpasswordauthenticationwasremovedonAugust13,2021.Pleaseuseapersonalaccesstokeninstead.ush的时候需要输入github的账户名和密码,而这里的大概意思就是密码验证在2021年8月13号被移除了,需要......
  • 在html5中播放RTSP/RTMP/HLS/HTTP视频流媒体的几种方案,并支持H.265
    经过多年的项目实战和研发经验的积累,总结了一下对于H5视频可视化在视频播放上如何做到无插件H5展示的方法,尤其是契合安防行业的方案;除了HTTP、WebSocket类的传输协议,其他是无法通用地传输到浏览器的,所以,如果要做一款通用的H5视频播放器,基本上就是一款HTTP/WebSocket协议的视频播放......
  • 安防监控视频云存储EasyCVR平台H.265转码功能更新:新增分辨率配置
    安防视频集中存储EasyCVR视频监控综合管理平台可以根据不同的场景需求,让平台在内网、专网、VPN、广域网、互联网等各种环境下进行音视频的采集、接入与多端分发。在视频能力上,视频云存储平台EasyCVR可实现视频实时直播、云端录像、视频云存储、视频存储磁盘阵列、录像回放与检索、......