当前位置:网站首页>删除排序数组中的重复项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
}
边栏推荐
- Leetcode basic programming: array
- Markdown edit syntax
- Detailed process of DC-1 range construction and penetration practice (DC range Series)
- Analyze the space occupied by the table according to segments, clusters and pages
- Several methods of capturing packets under CS framework
- Learn AI safety monitoring project from zero [attach detailed code]
- [bus interface] Axi interface
- UNET deployment based on deepstream
- How to configure PostgreSQL 12.9 to allow remote connections
- LeetCode-对链表进行插入排序
猜你喜欢

Typescript function details
![Introduction to Luogu 3 [circular structure] problem list solution](/img/fd/c0c5687c7e6e74bd5c911b27c3e19c.png)
Introduction to Luogu 3 [circular structure] problem list solution

Rhcsa --- work on the third day

Social media search engine optimization and its importance

数据库问题汇总

Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel

Idea autoguide package and autodelete package Settings
![[common error] the DDR type of FPGA device is selected incorrectly](/img/f3/be66bcfafeed581add6d48654dfe34.jpg)
[common error] the DDR type of FPGA device is selected incorrectly

Thinkphp內核工單系統源碼商業開源版 多用戶+多客服+短信+郵件通知

Save the CDA from the disc to the computer
随机推荐
C # picture display occupancy problem
奠定少儿编程成为基础学科的原理
Alibaba cloud polkit pkexec local rights lifting vulnerability
06 装饰(Decorator)模式
Cultivate primary and secondary school students' love for educational robots
Precipitate yourself and stay up late to sort out 100 knowledge points of interface testing professional literacy
记录一次Unity 2020.3.31f1的bug
What are the rules and trading hours of agricultural futures contracts? How much is the handling fee deposit?
Online incremental migration of DM database
win10 磁盘管理 压缩卷 无法启动问题
idea自動導包和自動删包設置
Pit encountered in win11 pytorch GPU installation
Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
培养中小学生对教育机器人的热爱之心
Mysql重点难题(2)汇总
C# 图片显示占用问题
Mathematical knowledge -- understanding and examples of fast power
DJB Hash
國產全中文-自動化測試軟件Apifox
[high speed bus] Introduction to jesd204b