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 ...
随机推荐
- 关于远程链接 redis的坑·
今天遇到了一个问题,在redis.conf 中 将 bind: 注释掉bind 127.0.0.1 仍然不行 其实是要把bind 127.0.0.1 改为 0.0.0.0 才行 下面附赠详细过程 查看 ...
- 最新 盛天网络java校招面经 (含整理过的面试题大全)
从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.盛天网络等10家互联网公司的校招Offer,因为某些自身原因最终选择了盛天网络.6.7月主要是做系统复习.项目复盘.Leet ...
- csu 1909: Perfect Chocolate
1909: Perfect Chocolate Submit Page Summary Time Limit: 3 Sec Memory Limit: 128 Mb Submi ...
- python+unittest框架第一天unittest之简单认识Test Fixure:测试固件【8月17更新】
20万的慢慢会实现的吧,hhh unittest框架,我就不在介绍了,百度有很详细的介绍. 我们只要了解: 1.unittest是单元测试框架 2.它提供用例组织与执行:在实际工作中案例可能有上百条, ...
- [转帖]如何保护你的 Python 代码 (一)—— 现有加密方案
如何保护你的 Python 代码 (一)—— 现有加密方案 Prodesire Python猫 1周前
- [转帖]拿小本本记下的Linux Shell常用技巧(一)
拿小本本记下的Linux Shell常用技巧(一) https://zhuanlan.zhihu.com/p/73361101 一. 特殊文件: /dev/null和/dev/tty Linux系统提 ...
- [转帖]为微软效力15年的微软前员工解释Windows 10为什么问题这么多
为微软效力15年的微软前员工解释Windows 10为什么问题这么多 https://www.cnbeta.com/articles/tech/892109.htm . 测试团队已经被裁撤 . 自动化 ...
- QSqlDatabase
QSqlDatabase 使用静态方法addDatabase来创建一个数据库连接. 如果你的程序中只有一个数据库连接,可以使用如下语句创建连接 QSqlDatabase db = QSqlDatab ...
- c# sqlite 导入,升级
导入sqlite库 1.下载nupkg 安装包 http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki 记得.net ...
- C 统计用户输入的总行数和字符长度
C 统计用户输入的总行数和字符长度 #include <stdio.h> #include <Windows.h> int main(void) { ]; ; ; printf ...