{ "question_id": 3, "question_title": "无重复字符的最长子串", "difficulty": "中等", "question_content": "

给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。

 

示例 1:

输入: s = \"abcabcbb\"
输出:
3
解释:
因为无重复字符的最长子串是 \"abc\",所以其长度为 3。

示例 2:

输入: s = \"bbbbb\"
输出:
1
解释:
因为无重复字符的最长子串是 \"b\",所以其长度为 1。

示例 3:

输入: s = \"pwwkew\"
输出:
3
解释:
因为无重复字符的最长子串是 \"wke\",所以其长度为 3。 \n请注意,你的答案必须是 子串 的长度,\"pwke\" 是一个子序列,不是子串。

示例 4:

输入: s = \"\"
输出:
0

 

提示:

", "topic_link": "https://bbs.csdn.net/topics/600471011", "cpp": "int hset[128];\nint lengthOfLongestSubstring(char *s)\n{\n\tint i = 0, j = 0;\n\tint m = 0;\n\tmemset(hset, 0, sizeof hset);\n\tfor (; s[j]; j++)\n\t{\n\t\ti = hset[s[j]] > i ? hset[s[j]] : i;\n\t\tm = m > j - i + 1 ? m : j - i + 1;\n\t\thset[s[j]] = j + 1;\n\t}\n\treturn m;\n}", "java": "class Solution {\n\tpublic int lengthOfLongestSubstring(String s) {\n\t\tint i = 0;\n\t\tint j = 0;\n\t\tint m = 0;\n\t\tMap hset = new HashMap<>();\n\t\tfor (; j < s.length(); j++) {\n\t\t\tint code = s.codePointAt(j);\n\t\t\tInteger o = hset.get(code);\n\t\t\tif (o != null && o > i) {\n\t\t\t\ti = o;\n\t\t\t}\n\t\t\tm = m > j - i + 1 ? m : j - i + 1;\n\t\t\thset.put(code, j + 1);\n\t\t}\n\t\treturn m;\n\t}\n}", "js": "\n/**\n * @param {string} s\n * @return {number}\n */\nvar lengthOfLongestSubstring = function(s) {\n let i = 0;\n let j = 0;\n let m = 0;\n let hset = {};\n for (;s[j];j++) {\n i = hset[s[j]] > i ? hset[s[j]] : i;\n m = m > j - i + 1 ? m : j - i + 1;\n hset[s[j]] = j + 1;\n }\n return m;\n};\n", "python": "class Solution:\n\tdef lengthOfLongestSubstring(self, s: str) -> int:\n\t\ti = 0\n\t\tj = 0\n\t\tm = 0\n\t\thset = {}\n\t\twhile j < len(s):\n\t\t\tchar = s[j]\n\t\t\tindex = hset.get(char)\n\t\t\tif index is not None and index > i:\n\t\t\t\ti = index\n\t\t\tm = m if m > j - i + 1 else j - i + 1\n\t\t\thset[char] = j + 1\n\t\t\tj += 1\n\t\treturn m\n# %%\ns = Solution()\nprint(s.lengthOfLongestSubstring('abcabcbb'))", "status": 1, "keywords": "哈希表,字符串,滑动窗口", "license": { "cpp": "csdn.net", "python": "csdn.net", "java": "csdn.net" }, "notebook": { "cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/2/2_cpp.ipynb?type=file", "python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/2/2_python.ipynb?type=file", "java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/2/2_java.ipynb?type=file" }, "notebook_enable": 1 }