leetcode 3. Longest Substring Without Repeating Characters
来源:互联网 发布:sql优化书籍推荐 编辑:程序博客网 时间:2024/06/08 03:07
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
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 subsequenceand not a substring.
way-2:用map,遍历s每一位 map[s[i]]存的是s[i]的下标,
如果找到重复的,就统计map长度,然后把begin到map[s[i]]的位全部清理出map。从map[s[i]] + 1 重新开始。
如果没有,就加入map
第一种的速度比较快!
class Solution {public: int lengthOfLongestSubstring(string s) { //way-1 if(s == "") return 0; int j = 0; //后面的指针 int ret = 1; for (int is = 0; is < s.length() - 1; is++) //is是前面的指针 { j++; string m1 = s.substr(is, j - is); while (m1.find(s[j]) == -1 && j < s.length()) { m1 = m1 + s[j]; j ++; } ret = max(ret, j - is); if(j < s.length()) { is = is + m1.find(s[j]); //m1.find(s[j])是 m1 中s[j]在的位数,m1是从is开始的.重新定位下一次开始的地点 } } return ret; //way-2 /* map<char,int> s_count; int maxlength = 0; int begin = 0; int i = 0; while (i < s.size()) { map<char,int>::iterator it = s_count.find(s[i]); if (it == s_count.end()) { s_count[s[i]] = i; } else { maxlength = max(maxlength, i - begin); for (int j = begin; j < it->second; j++) s_count.erase(s[j]); begin = it->second + 1; s_count[s[i]] = i; } i++; } return max(maxlength, i - begin); */ }};
阅读全文
0 0
- [LeetCode]3.Longest Substring Without Repeating Characters
- LeetCode 3.Longest Substring Without Repeating Characters
- LeetCode --- 3. Longest Substring Without Repeating Characters
- [Leetcode] 3. Longest Substring Without Repeating Characters
- [leetcode] 3.Longest Substring Without Repeating Characters
- [leetcode] 3. Longest Substring Without Repeating Characters
- [Leetcode]3. Longest Substring Without Repeating Characters
- leetcode 3. Longest Substring Without Repeating Characters
- 【Leetcode】3. Longest Substring Without Repeating Characters
- leetcode 3. Longest Substring Without Repeating Characters
- LeetCode-3.Longest Substring Without Repeating Characters
- LeetCode 3.Longest Substring Without Repeating Characters
- LeetCode - 3.Longest Substring Without Repeating Characters
- Leetcode 3. Longest Substring Without Repeating Characters
- 3. Longest Substring Without Repeating Characters LeetCode
- (Leetcode)3. Longest Substring Without Repeating Characters
- leetcode 3. Longest Substring Without Repeating Characters
- LeetCode-3. Longest Substring Without Repeating Characters
- Android6.0动态权限申请
- Session
- 马尔科夫随机场(MRF)与吉布斯分布(Gibbs)
- spacy库安装
- Dynpro程序抬头信息要求多值输入的解决方法
- leetcode 3. Longest Substring Without Repeating Characters
- centos 下安装gcc vim gdb
- LeetCode 142. Linked List Cycle II
- PLSQL Developer连接Oracle11g 64位数据库配置详解
- Android多语言切换,字体风格,简体转繁体
- android studio导入项目出错:Error:Execution failed for task ':app:processDebugManifest'. > Manifest merger
- Java实现哈夫曼树(HuffmanTree)
- github上最全的资源教程-前端涉及的所有知识体系
- Android中<meta-data>的使用