leetcode No3. Longest Substring Without Repeating Characters
来源:互联网 发布:刘德华救歌迷知乎 编辑:程序博客网 时间:2024/06/07 17:48
Question:
Given a string, find the length of the longest substring without repeating characters.
Ex:
Given "abcabcbb"
, the answer is "abc"
, which the length is 3.
Given "bbbbb"
, the answer is "b"
, with the length of 1.
Given "pwwkew"
, the answer is "wke"
, with the length of 3. Note that the answer must be a substring, "pwke"
is a subsequence and not a substring.
Algorithm:Hash Table、Two Pointers
1、给出i,j分别表示不重复的开头和结尾,max_length记录最大长度,2、j一直往右,同时用哈希表记录出现的字符,直到,有字符出现了两次,这时,i就应该到重复字符第一次出现的位置的下一位。
3、有字符出现两个次时,记录当前的子字符串长度cur_length,并和max_length作比较,如果比max_length大则更新。
Submitted Code:
class Solution {public: int lengthOfLongestSubstring(string s) { if(s.size()<2)return s.size(); int i=0; //begin int j=0; //end int cur_length=0; int max_length=0; map<char,int> hash; while(j<s.size()) { if(hash[s[j]]==0) { hash[s[j]]=1; j++; } else { while(s[i]!=s[j]) { hash[s[i]]=0; i++; } i++; j++; } cur_length = j-i; max_length = max_length>cur_length?max_length:cur_length; } return max_length; }};
0 0
- LeetCode : No3 Longest Substring Without Repeating Characters
- leetcode No3. Longest Substring Without Repeating Characters
- No3.Longest Substring Without Repeating Characters
- 【LeetCode从零单排(Java)】No3. Longest Substring Without Repeating Characters
- LeetCode: Longest Substring Without Repeating Characters
- LeetCode Longest Substring Without Repeating Characters
- LeetCode: Longest Substring Without Repeating Characters
- [Leetcode] Longest Substring Without Repeating Characters
- leetcode 23: Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- LeetCode 3 - Longest Substring Without Repeating Characters
- [LeetCode]Longest Substring Without Repeating Characters
- leetcode Longest Substring Without Repeating Characters
- 【leetcode】 Longest Substring Without Repeating Characters
- LeetCode-Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- [leetcode] Longest Substring Without Repeating Characters
- 0603 c语言函数
- CPU内部架构和工作原理
- Swagger UI教程 API 文档神器 搭配Node使用 web api 接口文档 mvc接口文档
- Xcode因为证书问题经常报的那些错
- 处理图片的旋转问题bug
- leetcode No3. Longest Substring Without Repeating Characters
- chinese restaurant process
- hdu 1238 substrings
- 湘潭oj_Rolling Variance1249(直接求)
- Defender 1.4.0.629解密研究
- web打印免费控件PAZU -- 四方在线WEB打印控件
- 欢迎使用CSDN-markdown编辑器
- Face2Face: Real-time Face Capture and Reenactment
- XML和JSON的区别