From 4a913d0dbd68c51085ba09908dc53f50a9477e29 Mon Sep 17 00:00:00 2001 From: H_G <23271365+simkakashi@users.noreply.github.com> Date: Thu, 6 Dec 2018 13:33:34 +0800 Subject: [PATCH 1/2] Update readme.md --- HughGuo/readme.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/HughGuo/readme.md b/HughGuo/readme.md index d6aa085..5d3aedf 100644 --- a/HughGuo/readme.md +++ b/HughGuo/readme.md @@ -1,3 +1,4 @@ # HughGuo贡献内容 -- 两数求和 \ No newline at end of file +- #2. 两数求和 +- #3. 无重复字符的最长子串 From 8148be9f52e7895fc50c06f140e0e46158ec865d Mon Sep 17 00:00:00 2001 From: H_G <23271365+simkakashi@users.noreply.github.com> Date: Fri, 7 Dec 2018 09:32:03 +0800 Subject: [PATCH 2/2] Add files via upload --- ...200\351\225\277\345\255\220\344\270\262.py" | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 "HughGuo/Leetcode-02/C003_\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262.py" diff --git "a/HughGuo/Leetcode-02/C003_\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262.py" "b/HughGuo/Leetcode-02/C003_\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262.py" new file mode 100644 index 0000000..c90a041 --- /dev/null +++ "b/HughGuo/Leetcode-02/C003_\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262.py" @@ -0,0 +1,18 @@ +class Solution: + def lengthOfLongestSubstring(self, s): + """ + :type s: str + :rtype: int + """ + temp = 0 + d = {} + start = 0 + for i in range(len(s)): + if s[i] in d and start <= d[s[i]] : + start = d[s[i]] +1 + temp = max(i-start+1,temp) + d[s[i]] = i + return temp + +## 维护两个字符串,一个是当前最大的字符串l,一个是当前字符串t,最初的时候这两个字符串都为空。每扫一个字符,如果这个字符不在字符串当中,就把当前字符串加上这个字符。如果在,当前字符串就不能再往前加字符了,然后比较当前字符串和当前最大字符串。再把当前字符串的开始地址替换掉因为不能有重复元素,##麻烦 +## 滑动窗口! 机智!