当前位置:网站首页>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)
}
边栏推荐
- 最近准备翻译外国优质文章
- 万亿热钱砸向太空经济,真的是一门好生意?
- 如何用jmeter做接口测试
- Circuit module analysis exercise 6 (switch)
- OpenJudge NOI 2.1 15:Counterfeit Dollar
- Ue4 Ue5 combine le plug - in de reconnaissance vocale de bureau pour la reconnaissance vocale
- How to download the software package of CDH version
- LM small programmable controller software (based on CoDeSys) note XVII: PTO pulse function block
- [eosio] eos/wax signature error is_ Canonical (c): signature is not canonical
- 【opencv450-samples】读取图像路径列表并保持比例显示
猜你喜欢

The applet draws a simple pie chart
![[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

Xampp重启后,MySQL服务就启动不了。

RepOptimizer: 其实是RepVGG2

What is Unified Extensible Firmware Interface (UEFI)?

String deformation (string case switching and realization)

ES6 --- 数值扩展、对象拓展

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

一位博士在华为的22年

1281_ FreeRTOS_ Implementation analysis of vtaskdelayuntil
随机推荐
How to solve the problem of SQL?
1281_ FreeRTOS_ Implementation analysis of vtaskdelayuntil
[opencv450 samples] inpaint restores the selected region in the image using the region neighborhood
Oracle -- table operation
Civil Aviation Administration: by 2025, China will initially build a safe, intelligent, efficient and green aviation logistics system
万亿热钱砸向太空经济,真的是一门好生意?
Multi modal data can also be Mae? Berkeley & Google proposed m3ae to conduct Mae on image and text data! The optimal masking rate can reach 75%, significantly higher than 15% of Bert
电路模块分析练习5(电源)
Use and difference between ue4\ue5 blueprint node delay and retroggable delay
Unity technical manual - color in life cycle coloroverlifetime-- speed color colorbyspeed
Es7/es9 -- new features and regularities
zabbix_server配置文件详解
The applet draws a simple pie chart
RepOptimizer: 其实是RepVGG2
As a programmer, how can we learn, grow and progress happily? (personal perception has nothing to do with technology)
Equivalence class, boundary value, application method and application scenario of scenario method
[modulebuilder] GP service realizes the intersection selection of two layers in SDE
STM32开发板+机智云AIoT+家庭监测控制系统
Common MySQL database functions and queries
Trillions of hot money smashed into the space economy. Is it really a good business?