当前位置:网站首页>自学golang【3.8使用go语言实现寻找最长不含有重复字符的子串】练习代码
自学golang【3.8使用go语言实现寻找最长不含有重复字符的子串】练习代码
2022-07-21 17:01:00 【立志考博士】
代码部分:
package main
import "fmt"
//寻找最长不含有重复字符的子串
//例如:abcabcbb:abc
//bbbbb:b
func length(s string) int { //定义一个函数,参数类型是string,函数返回值为int
last := make(map[rune]int)
start := 0
maxlength := 0
for i, ch := range []rune(s) { //[]rune是将s转为rune类型
if last, ok := last[ch]; ok && last >= start { //防止last不存在
start = last + 1
}
if i-start+1 > maxlength {
maxlength = i - start + 1
}
last[ch] = i
}
return maxlength
}
func main() {
fmt.Println(length("abcabcbb"))
fmt.Println(length("bbbbb"))
fmt.Println(length("我是李文杰"))
}
运行结果:
3
1
5
运行截图:
边栏推荐
- Move! What if you can't take the PMP Exam?
- Is it safe to open an account in flush? Ranking of China's top ten securities companies
- Redis的经典三问以及哨兵
- 使用 SUM 函数对区域中的数字求和
- MySQL存储过程
- Zusand of state management
- R language tests the significance of correlation coefficient: use cor.test function to calculate the value and confidence interval of correlation coefficient and its statistical significance (if the v
- EXCEL一些技巧
- 生产环境TiDB集群缩容TiKV操作步骤
- 深度学习之Caffe框架及制作数据源
猜你喜欢
grpc-middleware实现grpc调用重试
Installing MySQL on Linux (CentOS)
架构师进阶,微服务设计与治理的 16 条常用原则
Multiple worksheets under the same workbook have the same format
数据分析与挖掘1
excel 如何删除有颜色的行
CDH 6.1 environment construction graphic tutorial
Detailed construction process of airflow (personal test + summary)
9月备忘录
Verilog——74LVC161计数器
随机推荐
p[0]作为for循环的判断条件
百面机器学习总结
Advanced architects, 16 common principles of microservice design and Governance
9月备忘录
Verilog应用——24秒篮球计数器
No.js---基于V8和io_uring的JS运行时
同个一个工作簿下多个工作表相同格式
数据分析与挖掘1
資料分享|HC-05藍牙模塊資料
Linux(Centos)安装Mysql
Detailed explanation of wireless charging principle and Qi protocol [easy to understand]
PMP考生注意,这些地区考试将延期
为什么都说考完二建就要去准备一建,这几点你一定不知道!
Mysql REGEXP不区分大小写解决办法
Multiple worksheets under the same workbook have the same format
char short int等类型变量在内存中占用字节数
Explain pytorch visualization tool visdom in detail (I)
AttributeError:module ‘distutils’ has no attribute ‘version错误解决方法
Verilog——串行四位加法器和超前四位加法器74HC283
Finer grained useeffect