当前位置:网站首页>删除排序数组中的重复项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
}
边栏推荐
- Case sharing | intelligent Western Airport
- Mapping location after kotlin confusion
- 2022-003arts: recursive routine of binary tree
- 解析少儿编程中的动手搭建教程
- [bus interface] Axi interface
- Mapping settings in elk (8) es
- Mysql重点难题(2)汇总
- 数据库问题汇总
- Solution of DM database unable to open graphical interface
- Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知
猜你喜欢
Pytest learning ----- pytest Interface Association framework encapsulation of interface automation testing
Record the bug of unity 2020.3.31f1 once
C case of communication between server and client based on mqttnet
Ansible installation and use
CubeMx DMA笔记
Let genuine SMS pressure measurement open source code
Mysql database learning
農業生態領域智能機器人的應用
06 decorator mode
Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
随机推荐
【ClickHouse】How to create index for Map Type Column or one key of it?
关于Steam 教育的知识整理
Steam教育的实际问题解决能力
[graduation season · advanced technology Er] young people have dreams, why are they afraid of hesitation
Fasttext text text classification
Promise all()
Video cover image setting, put cover images into multiple videos in the simplest way
ansible安装与使用
Domestic all Chinese automatic test software apifox
培养中小学生对教育机器人的热爱之心
Analyzing the hands-on building tutorial in children's programming
Lay the foundation for children's programming to become a basic discipline
汇编语言中的标志位:CF、PF、AF、ZF、SF、TF、IF、DF、OF
C# 基于MQTTNet的服务端与客户端通信案例
Detailed process of DC-1 range construction and penetration practice (DC range Series)
Gin framework learning code
LeetCode-对链表进行插入排序
[Yu Yue education] autumn 2021 reference materials of Tongji University
Video multiple effects production, fade in effect and border background are added at the same time
Comp 250 parsing