Longest Substring Without Repeating Characters
来源:互联网 发布:tts软件 编辑:程序博客网 时间:2024/09/21 06:36
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 subsequence and not a substring.
class Solution {public: int lengthOfLongestSubstring(string s) { if(s.size() == 1) return 1; if(s.size() == 0) return 0; const char *ss = s.c_str(); set<char> SetS; set<char>::iterator it; int MaxS = 1; int CurrentS = 1; int FirstL = 0; int LastL = 1; SetS.insert(ss[FirstL]); for(int i=0; i< s.length();i++) { it = SetS.find(ss[LastL]); if(it !=SetS.end()) { if(CurrentS > MaxS) MaxS = CurrentS; for(int j = FirstL;;) { if(ss[LastL] == ss[j]) { FirstL = j+1; break; } else { j++; CurrentS--; SetS.erase(ss[j-1]); } } } else { SetS.insert(ss[LastL]); CurrentS++; } if(LastL == s.size() -1 ) break; LastL++; } if(CurrentS > MaxS) return CurrentS; return MaxS; }};注意事项:各种边界条件,坑爹的SET还有就是C++忘得差不多了,一道题写了3个小时,难受
阅读全文
0 0
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- 不会玩Linux系统怎么找女朋友?
- css怎么让div动起来,实现动画效果一直在动
- C语言malloc和free
- 【NOIP】时间将尽の模板集合
- hdu2063(最大权匹配匈牙利算法模板)
- Longest Substring Without Repeating Characters
- Centos 下安装redmine
- 【CS 1380】没有上司的舞会(树形dp+dfs)
- 《深入理解linux静态库与动态库》
- 2017年南宁网络赛 L 题 【最长非递减序列 + 思维】
- c++ 合并两个无序顺序表使其仍无序
- 奶牛送苹果
- JSON入门Java篇-2-JSON数据类型
- 路径