From 34000002c3e9e3714d50fd02e80a2cac29323982 Mon Sep 17 00:00:00 2001 From: Zong <1003160664@qq.com> Date: Wed, 28 Oct 2020 21:41:04 +0800 Subject: [PATCH 1/2] Update 3.longest-substring-without-repeating-characters.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加C++、Java语言支持 --- ...-substring-without-repeating-characters.md | 60 ++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/problems/3.longest-substring-without-repeating-characters.md b/problems/3.longest-substring-without-repeating-characters.md index a36daedb9..c85161145 100644 --- a/problems/3.longest-substring-without-repeating-characters.md +++ b/problems/3.longest-substring-without-repeating-characters.md @@ -66,7 +66,65 @@ https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ ## 代码 -代码支持:Python3 +代码支持:C++,Java,Python3 + + +C++ Code: + +```c++ +class Solution { +public: + int lengthOfLongestSubstring(string s) { + + int max_len = 0, start = 0; + int n = s.length(); + // + map mp; + + for(int i=0;i map = new HashMap<>(); + + for(int i=0;i Date: Thu, 29 Oct 2020 11:58:59 +0800 Subject: [PATCH 2/2] Update 3.longest-substring-without-repeating-characters.md --- ...ngest-substring-without-repeating-characters.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/problems/3.longest-substring-without-repeating-characters.md b/problems/3.longest-substring-without-repeating-characters.md index c85161145..b14da9627 100644 --- a/problems/3.longest-substring-without-repeating-characters.md +++ b/problems/3.longest-substring-without-repeating-characters.md @@ -49,7 +49,7 @@ https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ 2. 如果当前遍历到的字符出现过,则缩小窗口(左边索引向右移动),然后继续观察当前遍历到的字符; -3. 重复(1)(2),直到左边索引无法再移动; +3. 重复(1)(2),直到窗口内无重复元素; 4. 维护一个全局最大窗口 res,每次用出现过的窗口大小来更新结果 res,最后返回 res 获取结果; @@ -76,7 +76,7 @@ class Solution { public: int lengthOfLongestSubstring(string s) { - int max_len = 0, start = 0; + int ans = 0, start = 0; int n = s.length(); // map mp; @@ -88,12 +88,12 @@ public: { start = max(start, mp[alpha]+1); } - max_len = max(max_len, i-start+1); + ans = max(ans, i-start+1); // 字符位置 mp[alpha] = i; } - return max_len; + return ans; } }; ``` @@ -104,7 +104,7 @@ Java Code: ```java class Solution { public int lengthOfLongestSubstring(String s) { - int max_len = 0, start = 0; + int ans = 0, start = 0; int n = s.length(); // Map map = new HashMap<>(); @@ -116,12 +116,12 @@ class Solution { { start = Math.max(start, map.get(alpha)+1); } - max_len = Math.max(max_len, i-start+1); + ans = Math.max(ans, i-start+1); // 字符位置 map.put(alpha, i); } - return max_len; + return ans; } } ```