当前位置:网站首页>删除排序数组中的重复项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
}
边栏推荐
- idea自動導包和自動删包設置
- Mapping settings in elk (8) es
- Steam教育的实际问题解决能力
- Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel
- fastText文本分类
- ansible安装与使用
- [understand one article] FD_ Use of set
- Realize the function of data uploading
- Case sharing | intelligent Western Airport
- The underlying principle of go map (storage and capacity expansion)
猜你喜欢
農業生態領域智能機器人的應用
正大美欧4的主账户关注什么数据?
Win10 disk management compressed volume cannot be started
Rhcsa --- work on the third day
解决:代理抛出异常错误
面试会问的 Promise.all()
Research on the security of ognl and El expressions and memory horse
ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification
Markdown编辑语法
数学知识——快速幂的理解及例题
随机推荐
LeetCode-归并排序链表
Rhcsa --- work on the fourth day
洛谷入门3【循环结构】题单题解
记录一次Unity 2020.3.31f1的bug
Cultivate primary and secondary school students' love for educational robots
C # picture display occupancy problem
VMware installation win10 reports an error: operating system not found
UNET deployment based on deepstream
C - derived classes and constructors
Cubemx DMA notes
Leetcode merge sort linked list
Geotrust OV Multi - Domain Domain SSL Certificate rmb2100 per year contains several Domain names?
Super detailed pycharm tutorial
DC-1靶场搭建及渗透实战详细过程(DC靶场系列)
Idea autoguide package and autodelete package Settings
Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market
Mouse events in JS
数据库问题汇总
Line by line explanation of yolox source code of anchor free series network (7) -- obj in head_ loss、Cls_ Loss and reg_ Calculation and reverse transmission of loss I
Express logistics quick query method, set the unsigned doc No. to refresh and query automatically