当前位置:网站首页>golang Make a list of intervals with sequential numbers
golang Make a list of intervals with sequential numbers
2022-06-25 21:59:00 【大魔法师云中君】
package main
import (
"log"
"sort"
"strconv"
)
func main() {
a := []int{
1, 2, 3, 9, 8, 7, 6, 11}
// need sorted slice
sort.Slice(a, func(i, j int) bool {
return a[i] < a[j]
})
log.Println(a)
var result []string
rangeStart, previousNumber := a[0], a[0]
tmp := ""
for _, number := range a[1:] {
log.Println(number)
if number == previousNumber+1 {
previousNumber = number
} else {
if rangeStart == previousNumber {
tmp = strconv.Itoa(rangeStart)
} else {
tmp = strconv.Itoa(rangeStart) + "-" + strconv.Itoa(previousNumber)
}
result = append(result, tmp)
rangeStart, previousNumber = number, number
}
}
if rangeStart == previousNumber {
tmp = strconv.Itoa(rangeStart)
} else {
tmp = strconv.Itoa(rangeStart) + "-" + strconv.Itoa(previousNumber)
}
result = append(result, tmp)
log.Println(result)
}
边栏推荐
- Exclusive or operator simple logic operation a^=b
- 作为一个程序员我们如何快乐的学习成长进步呢?(个人感悟和技术无关)
- Actual combat: how to quickly change font color in typera (blog sharing - perfect) -2022.6.25 (solved)
- 做接口测试,这3种工具到底什么时候用?
- Comp2913 database
- 首个大众可用PyTorch版AlphaFold2复现,哥大开源OpenFold,star量破千
- 剑指 Offer 46. 把数字翻译成字符串(DP)
- No absurd tea applet - rule change
- Flex & Bison 开始
- Day3 data types and operators summary and job
猜你喜欢

Fegin client entry test

UE4\UE5 蓝图节点Delay与Retriggerable Delay的使用与区别
![[modulebuilder] GP service realizes the intersection selection of two layers in SDE](/img/4a/899a3c2a0505d2ec2eaae97a3948c9.png)
[modulebuilder] GP service realizes the intersection selection of two layers in SDE

What is Unified Extensible Firmware Interface (UEFI)?

Kubernetes cluster construction of multiple ECS

ES6-- 模板字符串、对象的简化写法、箭头函数

2022年河南省第一届职业技能大赛网络安全项目试题

万亿热钱砸向太空经济,真的是一门好生意?

剑指 Offer 46. 把数字翻译成字符串(DP)

ES6 -- formal parameter setting initial value, extension operator, iterator, and generating function
随机推荐
Unity technical manual - particle emission and life cycle velocity sub module
LM small programmable controller software (based on CoDeSys) note XVII: PTO pulse function block
问题记录与思考
The sum of logarithms in group 52--e of Niuke Xiaobai monthly race (two points)
Idea common plug-ins
Idea FAQ collection
ES6 -- formal parameter setting initial value, extension operator, iterator, and generating function
1281_ FreeRTOS_ Implementation analysis of vtaskdelayuntil
Trillions of hot money smashed into the space economy. Is it really a good business?
万亿热钱砸向太空经济,真的是一门好生意?
[opencv450 samples] inpaint restores the selected region in the image using the region neighborhood
User interaction scanner usage Advanced Edition example
Sword finger offer 46 Translate numbers to strings (DP)
Jupiter notebook common shortcut keys
MySQL queries data by day, week, month, quarter and year
The Ping class of unity uses
New network security competition of the secondary vocational group in 2022
String deformation (string case switching and realization)
Fegin client entry test
Typora writing DOS commands