当前位置:网站首页>2021-06-24: find the length of the longest non repeating character substring in a string.
2021-06-24: find the length of the longest non repeating character substring in a string.
2022-06-24 08:27:00 【Fuda scaffold constructor's daily question】
2021-06-24: In a string , The longest non repeating character substring length .
Fuda answer 2021-06-24:
Method 1 : The sliding window . Natural intelligence .
When not repeated , Right pointer moves right ; When I repeat , Move the left pointer to the right .
Method 2 : Find the rightmost non repeating position .
map:key Is the value ,value It's an array number , Initial value value All are -1.
Time complexity :O(N). Spatial complexity :O( Number of different characters ).
The code to use golang To write . The code is as follows :
package main
import "fmt"
func main() {
s := "moonfdd"
ret1 := lengthOfLongestSubstring1(s)
fmt.Println(ret1)
ret2 := lengthOfLongestSubstring2(s)
fmt.Println(ret2)
}
// Method 1 : The sliding window . Natural intelligence .
func lengthOfLongestSubstring1(s string) int {
// Hash set , Record whether each character appears
m := map[byte]int{}
n := len(s)
// Right pointer , The initial value is -1, It's like we're on the left side of the left bound of the string , It's not moving yet
rk, ans := -1, 0
for i := 0; i < n; i++ {
if i != 0 {
// The left pointer moves one space to the right , Remove a character
delete(m, s[i-1])
}
for rk+1 < n && m[s[rk+1]] == 0 {
// Keep moving the right pointer
m[s[rk+1]]++
rk++
}
// The first i To rk A character is a very long non repeating character substring
ans = getMax(ans, rk-i+1)
}
return ans
}
// Method 2 : Find the rightmost non repeating position .
func lengthOfLongestSubstring2(s string) int {
if len(s) == 0 {
return 0
}
indexList := make([]int, 256)
for i := 0; i < 256; i++ {
indexList[i] = -1
}
N := len(s)
ans := 0
p := -1
for i := 0; i < N; i++ {
p = getMax(p, indexList[s[i]])
ans = getMax(ans, i-p)
indexList[s[i]] = i
}
return ans
}
func getMax(a int, b int) int {
if a > b {
return a
} else {
return b
}
}
func getMin(a int, b int) int {
if a > b {
return a
} else {
return b
}
}The results are as follows :
边栏推荐
- Promise的使用場景
- 工控机防破解
- 12-- merge two ordered linked lists
- Review SGI STL secondary space configurator (internal storage pool) | notes for personal use
- 新技术实战,一步步用Activity Results API封装权限申请库
- [introduction to point cloud dataset]
- Chart list Performance Optimization: minimum resource consumption in the visualization area
- Search and recommend those things
- 51 single chip microcomputer_ External interrupt and timer / Counter interrupt
- Markdown 实现文内链接跳转
猜你喜欢

Small sample fault diagnosis - attention mechanism code - Implementation of bigru code parsing

Use of swift basic closure /block (source code)

FPGA的虚拟时钟如何使用?

Application of JDBC in performance test

Introduction to RCNN, fast RCNN and fast RCNN

自动化测试的未来趋势

2022年流动式起重机司机特种作业证考试题库及在线模拟考试

io模型初探

1279_VMWare Player安装VMWare Tools时VSock安装失败解决

小样本故障诊断 - 注意力机制代码 - BiGRU代码解析实现
随机推荐
Vscode topic recommendation
RCNN、Fast-RCNN、Faster-RCNN介绍
Getting started with ffmpeg
Five level classification of loans
貸款五級分類
你还只知道测试金字塔?
51单片机_外部中断 与 定时/计数器中断
2021-03-11 comp9021 class 8 notes
独立站运营中如何提升客户留存率?客户细分很重要!
Utilisation de la fermeture / bloc de base SWIFT (source)
Swift foundation features unique to swift
Introduction to RCNN, fast RCNN and fast RCNN
Review SGI STL secondary space configurator (internal storage pool) | notes for personal use
Upgrade Mysql to the latest version (mysql8.0.25)
Application of JDBC in performance test
论文笔记: 多标签学习 DM2L
疫情下更合适的开发模式
Blue Bridge Cup_ Queen n problem
Analysis of abnormal problems in domain name resolution in kubernetes
Industrial computer anti cracking