Leetcode题解-3. Longest Substring Without Repeating Characters
来源:互联网 发布:nc数据下载 编辑:程序博客网 时间:2024/05/29 18:05
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 subsequence and not a substring.
思路:用一个大小为128的int数组,0代表这个符号没有出现,1代表出现了,从字符串头开始读起,把每个字符用对应的ascall码装填到数组里,当发现当前字符已经存在,寻找该字符在该字符串第一次出现的位置,把字符串头到该位置之间的子串删除,从该位置的下一位重新开始读,直到字符串的结尾。
该思路的时间复杂度是O(n^2),需要128*sizeof(int)的空间
int lengthOfLongestSubstring(string s) { int appear[127] = {0}; int flag = 0; int max = 0, current = 0; while(flag != s.size()){ if(appear[s[flag] ] == 1){ if(max < current) max = current; current = 0; memset(appear, 0, 127*sizeof(int)); int cut = s.find(s[flag]); s.erase(0, cut+1); flag = 0; } else{ appear[s[flag]] = 1; current ++; flag++; } } if(max < current) max = current; return max; }
阅读全文
0 0
- 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题解--3. Longest Substring Without Repeating Characters
- 3. Longest Substring Without Repeating Characters : LeetCode 题解
- Leetcode题解(Python): 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 Algorithms 3. Longest Substring Without Repeating Characters 题解
- leetcode题解-3. Longest Substring Without Repeating Characters
- 3. Longest Substring Without Repeating Characters LeetCode题解
- LeetCode题解(Week 16):3. Longest Substring Without Repeating Characters
- LeetCode 3. Longest Substring Without Repeating Characters题解python
- Leetcode题解-3. Longest Substring Without Repeating Characters
- 3. Longest Substring Without Repeating Characters题解
- [LeetCode]3.Longest Substring Without Repeating Characters
- tomcat添加manager-gui
- Hololens API解析Input-GestureSettings/InteractionSourceKind
- 使用phpStudy配置composer依赖的优化
- hibernate session的一级缓存,二级缓存以及查询缓存
- POJ 3070 Fibonacci
- Leetcode题解-3. Longest Substring Without Repeating Characters
- bzoj 1615: [Usaco2008 Mar]The Loathesome Hay Baler麻烦的干草打包机
- 如何平衡工作与家庭
- HDU 6198 number number number(斐波那契)
- JQUERY中的事件处理:return false、阻止默认行为、阻止冒泡以及兼容性问题
- weblogic10与myeclipse开发环境的集成
- SSH03---实现用户管理组功能
- MySQL(1)
- 春江花月夜有感