golang 2 ways to delete an element from a slice
2 ways to delete an element from a slice

Fast version (changes order)
a := []string{"A", "B", "C", "D", "E"}
i := 2
// Remove the element at index i from a.
a[i] = a[len(a)-1] // Copy last element to index i.
a[len(a)-1] = "" // Erase last element (write zero value).
a = a[:len(a)-1] // Truncate slice.
fmt.Println(a) // [A B E D]
The code copies a single element and runs in constant time.
Slow version (maintains order)
a := []string{"A", "B", "C", "D", "E"}
i := 2
// Remove the element at index i from a.
copy(a[i:], a[i+1:]) // Shift a[i+1:] left one index.
a[len(a)-1] = "" // Erase last element (write zero value).
a = a[:len(a)-1] // Truncate slice.
fmt.Println(a) // [A B D E]
The code copies len(a) - i - 1 elements and runs in linear time.
golang 2 ways to delete an element from a slice的更多相关文章
- Why Go? – Key advantages you may have overlooked
Why Go? – Key advantages you may have overlooked yourbasic.org/golang Go makes it easier (than Java ...
- Golang 学习资料
资料 1.How to Write Go Code https://golang.org/doc/code.html 2.A Tour of Go https://tour.golang.org/li ...
- golang(10)interface应用和复习
原文链接 http://www.limerence2017.com/2019/10/11/golang15/ interface 意义? golang 为什么要创造interface这种机制呢?我个人 ...
- Save Update saveOrUpdate delete
参考:Hibernate Session的saveOrUpdate()方法 saveOrUpdate与Update的作用 Hibernate Delete query Hibernate Basics ...
- [Swift]LeetCode740. 删除与获得点数 | Delete and Earn
Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...
- 说说不知道的Golang中参数传递
本文由云+社区发表 导言 几乎每一个C++开发人员,都被面试过有关于函数参数是值传递还是引用传递的问题,其实不止于C++,任何一个语言中,我们都需要关心函数在参数传递时的行为.在golang中存在着m ...
- 740. Delete and Earn
Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...
- [LeetCode]Delete and Earn题解(动态规划)
Delete and Earn Given an array nums of integers, you can perform operations on the array. In each op ...
- leetcode笔记(六)740. Delete and Earn
题目描述 Given an array nums of integers, you can perform operations on the array. In each operation, yo ...
随机推荐
- React中的setState到底发生了什么?
https://yq.aliyun.com/ziliao/301671 https://segmentfault.com/a/1190000014498196 https://blog.csdn.ne ...
- easyui 解决连弹两个dialog时候,第二个dialog居中问题
$('#showDivSecond').dialog('center'); (该方法自1.3.1版开始可用)
- 京东宙斯平台使用方法(accesstoken,appkey,appsecret参数和SDK的获取)
1.注册成为开发者 链接:https://dev.jd.com/ 2.创建应用 3.获取appsrecet和appkey,SDK(获取) 新建应用之后在左边应用证书栏位可以查看到appkey,apps ...
- 日常工作问题解决:使用vmvare克隆centos6虚拟机造成无eth0的解决办法
目录 一.问题描述 样本虚拟机配置有两张网卡,eth0.eth1,使用vmvare克隆虚拟机后,复制的虚拟机,没有网卡eth0,eth1,而是有eth2.eth3,如下所示: 二.原因分析 复制系统中 ...
- 数位dp踩坑
前言 数位DP是什么?以前总觉得这个概念很高大上,最近闲的没事,学了一下发现确实挺神奇的. 从一道简单题说起 hdu 2089 "不要62" 一个数字,如果包含'4'或者'62', ...
- VMware安装windows2003
一.安装vm 这一项大家应该都会,网上也有很多教程. 二.搭建Windows server 2003 1.镜像下载- 2.虚拟机安装 首先是新建虚拟机,我选的是自定义,也可以选典型 第一步默认下一步, ...
- BFS练习
1. 给定$d,k$, 求最小的被$d$整除, 且各数位仅有$k$和$0$组成的数. $(1\le k\le 9,1\le n\le 1e6)$ 从高位到低位$BFS$, BFS求出字典序最小的方案. ...
- jvm的内存区域介绍
什么是jvm? JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的 ...
- 转------深入理解--Java按值传递和按引用传递
引言 最近刷牛客网上的题目时碰到不少有关Java按值传递和按引用传递的问题,这种题目就是坑呀,在做错了n次之后,查找了多方资料进行总结既可以让自己在总结中得到提高,又可以让其他人少走弯路.何乐而不为? ...
- 微信小微商户申请入驻 .NET C#实现微信小微商户进件API
微信小微商户申请入驻 .NET C#实现微信小微商户进件API官方小微商户专属接口文档 微信支付SDK 微信支付官方SDK与DEMO下载 图片上传 图片上传接口API文档 证书下载 证书下载接口API ...