当前位置:网站首页>Leetcode. 3. Longest substring without repeated characters - more than 100% solution
Leetcode. 3. Longest substring without repeated characters - more than 100% solution
2022-07-06 13:36:00 【Li bohuan】
subject : Given a string s
, Please find out that there are no duplicate characters in it Longest substrings The length of .
Example 1:
Input : s = "abcabcbb" Output : 3 explain : Because the longest substring without repeating characters is"abc", So its The length is 3.
Input : s = "bbbbb" Output : 1 explain : Because the longest substring without repeating characters is "b", So its length is 1
The code is as follows :
public int lengthOfLongestSubstring(String s) {
if(s == null || s.equals("")){
return 0;
}
// With i How far to the left do you push the end without repeating ( consider a The location of the last occurrence and i-1 How far can we push )
char[] chars = s.toCharArray();
int n = chars.length;
//0-255 Of ASCII code use 256 The length array can represent all characters ASCii Code a byte The longest 256 But now it is defined 128 Characters
//map[i] = v representative i This ascii Code last appeared in v Location
//char Put characters in map The array will be automatically converted to int type Its value is char Character ascii Code value such as 'a' Namely 97
int[] map = new int[128];
for(int i = 0; i < map.length; i++){
map[i] = -1;// Means that all characters are in -1 The position has appeared
}
int ans = 1;// Initial answer
int pre = 1;// How long did the previous position push to the left ( Including myself )
map[chars[0]] = 0;// The first character is in
for(int i = 1; i < n; i++){
// Consider the character char[i] Where it was last seen
// for example adcba 01234 Then length is character char[i] The present position i Subtract the last position map[char[i]]
// 4 - 0 = 4 That is to say dcba
int p1 = i -map[chars[i]];
// Consider how much the previous position pushed to the left Add yourself Is the position you can push
int p2 = pre + 1;
// The minimum value of the two is the length that the current position can be pushed forward
int cur = Math.min(p1,p2);
// Take the maximum of all the answers
ans = Math.max(ans,cur);
// Continue the cycle later , So update pre and char[i] Where it was last seen
pre = cur;
map[chars[i]] = i;
}
return ans;
}
The main idea is dynamic planning , Considering that i How far can the ending character be pushed forward , This is related to how far the previous character can be pushed forward , Use an array map Store the last occurrence of this character , Initialize to -1, In order to calculate the distance , The calculation formula is unified .
边栏推荐
- Caching mechanism of leveldb
- View UI plus releases version 1.1.0, supports SSR, supports nuxt, and adds TS declaration files
- 【毕业季·进击的技术er】再见了,我的学生时代
- Introduction and use of redis
- 一段用蜂鸣器编的音乐(成都)
- Alibaba cloud microservices (IV) service mesh overview and instance istio
- 8.C语言——位操作符与位移操作符
- 关于双亲委派机制和类加载的过程
- (super detailed II) detailed visualization of onenet data, how to plot with intercepted data flow
- 甲、乙机之间采用方式 1 双向串行通信,具体要求如下: (1)甲机的 k1 按键可通过串行口控制乙机的 LEDI 点亮、LED2 灭,甲机的 k2 按键控制 乙机的 LED1
猜你喜欢
仿牛客技术博客项目常见问题及解答(三)
C language Getting Started Guide
Questions and answers of "signal and system" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
2. C language matrix multiplication
4.二分查找
Questions and answers of "Fundamentals of RF circuits" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
7.数组、指针和数组的关系
C language Getting Started Guide
魏牌:产品叫好声一片,但为何销量还是受挫
仿牛客技术博客项目常见问题及解答(二)
随机推荐
抽象类和接口的区别
仿牛客技术博客项目常见问题及解答(二)
Pit avoidance Guide: Thirteen characteristics of garbage NFT project
Tyut outline of 2022 database examination of Taiyuan University of Technology
9.指针(上)
4.二分查找
Alibaba cloud microservices (IV) service mesh overview and instance istio
2. C language matrix multiplication
2.C语言初阶练习题(2)
The latest tank battle 2022 full development notes-1
C language to achieve mine sweeping game (full version)
【九阳神功】2016复旦大学应用统计真题+解析
4.分支语句和循环语句
学编程的八大电脑操作,总有一款你不会
hashCode()与equals()之间的关系
透彻理解LRU算法——详解力扣146题及Redis中LRU缓存淘汰
5. Function recursion exercise
【毕业季·进击的技术er】再见了,我的学生时代
View UI plus releases version 1.1.0, supports SSR, supports nuxt, and adds TS declaration files
The latest tank battle 2022 - full development notes-3