leetcode -- Longest Substring Without Repeating Characters -- 简单要知道思路
来源:互联网 发布:淘宝有意思的店铺 编辑:程序博客网 时间:2024/06/07 03:06
https://leetcode.com/problems/longest-substring-without-repeating-characters/
思路参考http://www.cnblogs.com/zuoyuan/p/3785840.html
用双指针i,j. i是substring的开头,j是substring的结尾。i固定,用j来往后scan。边scan边update 哈希表,知道j遇到一个重复的字母,然后从哈希表中获取此重复值在substring的index,然后删除哈希表中index之前的所有元素。更新这个重复字母的哈希值为j,更新i为index +1.一直到j == len(s).期间注意给maxlen赋值。
这里其实也可以不用哈希表,用一个string就可以了
还要注意最后j == lens(s)跳出while循环之后,还要做一次与maxlen的判断。例如s = ‘abc’
class Solution(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtype: int """ mydict = {} maxlen = -1 i,j = 0,0 while j< len(s): if s[j] not in mydict: mydict[s[j]] = j else: maxlen = max(j - i, maxlen) ind = mydict[s[j]] mydict[s[j]] = j for k in xrange(i, ind): del mydict[s[k]] i = ind + 1 j += 1 maxlen = max(j - i, maxlen) return maxlen
0 0
- leetcode -- Longest Substring Without Repeating Characters -- 简单要知道思路
- leetCode 3. Longest Substring Without Repeating Characters 解法与思路
- Leetcode 3. Longest Substring Without Repeating Characters思路解析
- 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 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
- 有关FragmentDialog的细节问题
- Java 中 StringBuilder 在高性能用法总结
- 输入H.264流,输出封装格式流
- GridView网格视图
- ARM裸机开发学习方法
- leetcode -- Longest Substring Without Repeating Characters -- 简单要知道思路
- 定制eclipse的代码提示
- $.grep()函数详解——JQuery
- Codeforces Round #334 (Div. 2) B. More Cowbell
- 关于Oracle有两个或者多个实例时,用SID来区分目前oralce使用的是哪个实例。
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- struts2如何获取Request-Session-ServletContext-Response
- UI之textField、Button的创建方法及各属性
- ARM裸机开发快速体验