当前位置:网站首页>删除排序数组中的重复项go语言实现
删除排序数组中的重复项go语言实现
2022-07-02 04:58:00 【Jimmy_jimi】
目标:删除排序数组中的重复项
给你一个 升序排列 的数组 nums ,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。元素的 相对顺序 应该保持 一致 。
由于在某些语言中不能改变数组的长度,所以必须将结果放在数组nums的第一部分。更规范地说,如果在删除重复项之后有 k 个元素,那么 nums 的前 k 个元素应该保存最终结果。
将最终结果插入 nums 的前 k 个位置后返回 k 。
不要使用额外的空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。
作者:力扣 (LeetCode)
链接:https://leetcode.cn/leetbook/read/top-interview-questions-easy/x2gy9m/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
结果产出:
//双指针
func removeDuplicates(nums []int) int {
//len()取数组长度
if (len(nums) == 0){
return 0
}
slow,fast := 0,0
for ; fast < len(nums); fast++{
if (nums[fast] != nums[slow]){
slow++
nums[slow] = nums[fast]
}
}
return slow+1
}
边栏推荐
- 农业生态领域智能机器人的应用
- TypeScript函数详解
- Typescript function details
- Summary of common string processing functions in C language
- TypeScript类的使用
- Idea autoguide package and autodelete package Settings
- Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel
- oracle 存储过程与job任务设置
- 汇编语言中的标志位:CF、PF、AF、ZF、SF、TF、IF、DF、OF
- DJB Hash
猜你喜欢

Leetcode- insert and sort the linked list

農業生態領域智能機器人的應用

Cannot activate CONDA virtual environment in vscode

AcrelEMS高速公路微电网能效管理平台与智能照明解决方案智慧点亮隧道

DMA Porter

将光盘中的cda保存到电脑中

Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market

Mysql表insert中文变?号的问题解决办法

Idea automatic package import and automatic package deletion settings

How to configure PostgreSQL 12.9 to allow remote connections
随机推荐
Use of typescript classes
Mathematical knowledge -- understanding and examples of fast power
Markdown edit syntax
geotrust ov多域名ssl证书一年两千一百元包含几个域名?
DC-1靶场搭建及渗透实战详细过程(DC靶场系列)
[high speed bus] Introduction to jesd204b
06 装饰(Decorator)模式
Orthogonal test method and function diagram method for test case design
國產全中文-自動化測試軟件Apifox
Gin framework learning code
Thinkphp Kernel wo system source Commercial Open source multi - user + multi - Customer Service + SMS + email notification
DJB Hash
Ansible installation and use
Application d'un robot intelligent dans le domaine de l'agroécologie
Embedded-c language-9-makefile/ structure / Consortium
List of common bugs in software testing
C case of communication between server and client based on mqttnet
初学爬虫-笔趣阁爬虫
数学知识(欧拉函数)
Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel