leetcode第三题:无重复字符的最长子串
2018-12-03 11:36
369 查看
class Solution(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtype: int """ start = 0 max_len = 0 d = {} for i, c in enumerate(s): if c in d and d[c] >= start: max_len = max(max_len, i - start) start = d[c] + 1 d[c] = i return max(max_len, len(s) - start) print(Solution.lengthOfLongestSubstring('self','asdasd'))
解释:for i, c in enumerate(s):中的i是字典中的一个下标,从0开始,c就表示在字符串s中的每一个字母,
然后递归调用,找到第二次出现的一个字母的位置减去这个字母第一次出现的位置,找到最大的相减的长度。max
相关文章推荐
- leetcode第三题: 输出不包含重复字母的最长子串
- leetcode 395. 至少有K个重复字符的最长子串
- leetcode 第三题 最长不重复子串
- [LeetCode] Longest Substring Without Repeating Characters 无重复字符的最长子串
- leetcode(3)无重复字符的最长子串
- LeetCode 3. 无重复字符的最长子串
- 【LeetCode】3.无重复字符的最长子串 结题报告 (C++)
- [leetcode]获取字符串中字符不重复的最长子串的长度
- 【LeetCode002-003算法/编程练习C++】---Add Two Numbers||寻找最长无重复字母子串。//第三题之后有空要再想一想…
- 【leetcode】第3题:求最长不重复(字符不重复)的子串的长度
- [leetcode]无重复字符的最长子串
- 【Leetcode一刷】3.无重复字符的最长子串
- Leetcode 3. 无重复字符的最长子串
- Leetcode:395至少有K个重复字符的最长子串(C++)
- [leetcode]Longest Substring Without Repeating Characters【最长不重复字符子串】
- Leetcode 刷题(python)之 无重复字符的最长子串
- LeetCode 无重复字符的最长子串
- 转 leetcode ——无重复字符的最长子串
- 【LeetCode】3. 无重复字符的最长子串(Longest Substring Without Repeating Characters,24.2%,中等)
- LeetCode: 3_Longest Substring Without Repeating Characters | 求没有重复字符的最长子串的长度 | Medium