当前位置:网站首页>Self study golang [3.8 use go language to find the longest substring without repeated characters] exercise code
Self study golang [3.8 use go language to find the longest substring without repeated characters] exercise code
2022-07-22 08:35:00 【Aspire to be a doctor】
Code section :
package main
import "fmt"
// Find the longest substring that does not contain duplicate characters
// for example :abcabcbb:abc
//bbbbb:b
func length(s string) int { // Define a function , Parameter type is string, The return value of the function is int
last := make(map[rune]int)
start := 0
maxlength := 0
for i, ch := range []rune(s) { //[]rune Yes, it will s To rune type
if last, ok := last[ch]; ok && last >= start { // prevent last non-existent
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(" I'm Li Wenjie "))
}
Running results :
3
1
5
Run a screenshot :
边栏推荐
- 华泰证券账户开户安全吗,手机上就可以办理的吗?
- The use of comma expression in C language
- 七/四/五层网络模型
- CDH 6.1 environment construction graphic tutorial
- Sum the numbers in the region using the sum function
- Redis' classic three questions and sentinel
- MySQL transaction
- Calculation of storage capacity and address space
- Error: L6200E: Symbol keyflag multiply defined (by main.o and key.o).
- 编码之前先保证你的硬件稳定连接!!!
猜你喜欢
在 EXCEL 中,“插入已剪切单元格”的快捷键
Self study golang [Chapter 1: basic knowledge of go language] Why do you want to learn go language? The relationship between go language and C language? Founder of go language? What are the characteri
张驰咨询:六西格玛如何帮助公司减少客户投诉
软考中级【数据库系统工程师】第0章:如何自学备考,考试介绍考什么,备考教材,上午和下午的体型分数分布,备考课程链接,个人备考感谢
科技创造价值 | 云扩科技荣登真榜·中国科技创新品牌榜TOP100
职业交易者自用多年的终极突破策略分享(附全套交易模板)
自学golang【3.3go语言的循环语句】for循环语法结构,省略初始条件,省略递增条件,省略结束条件的应用
The difference between RDB and AOF in the persistence mode of redis
自学golang【3.2go语言变量的类型与条件语句,常量,变量,枚举,if语句与switch语句的应用】
自学golang【3.8使用go语言实现寻找最长不含有重复字符的子串】练习代码
随机推荐
张驰咨询:某包装印刷集团第五期六西格玛项目通过专家评审
9月备忘录
量化交易日记-2021年02月总结
pytorch学习(一):线性回归
How to add text before and after text in batch in Excel
职业交易者自用多年的终极突破策略分享(附全套交易模板)
华泰证券账户开户安全吗,手机上就可以办理的吗?
Error: L6200E: Symbol keyflag multiply defined (by main.o and key.o).
Error: L6200E: Symbol keyflag multiply defined (by main.o and key.o).
VS2022无法使用scanf的解决办法,‘scanf‘: This function or variable may be unsafe. Consider using scanf_s instea
房屋排序法
Redis的持久化方式RDB和AOF的区别
No.js--- based on V8 and IO_ Running JS runtime
p[0]作为for循环的判断条件
Detailed construction process of airflow (personal test + summary)
STM32 SPI reading data is inaccurate, only the first time is correct, and the latter is not correct
农产品期货怎么开户,找谁能开光大期货啊?
How to delete colored lines in Excel
自学golang【第一章:go语言基础知识】为什么要学习go语言?go语言与c语言的关系?go语言创始人?go语言的特性有哪些?go语言适合做什么?国内外有哪些企业或项目使用go语言?
【flex布局快速上手】快速理解flex布局用法,通过常见的四个布局案例解释【详细注释,一看就会】