File tree Expand file tree Collapse file tree 1 file changed +60
-2
lines changed
Expand file tree Collapse file tree 1 file changed +60
-2
lines changed Original file line number Diff line number Diff line change @@ -49,7 +49,7 @@ https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
4949
50502 . 如果当前遍历到的字符出现过,则缩小窗口(左边索引向右移动),然后继续观察当前遍历到的字符;
5151
52- 3 . 重复(1)(2),直到左边索引无法再移动 ;
52+ 3 . 重复(1)(2),直到窗口内无重复元素 ;
5353
54544 . 维护一个全局最大窗口 res,每次用出现过的窗口大小来更新结果 res,最后返回 res 获取结果;
5555
@@ -66,7 +66,65 @@ https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
6666
6767## 代码
6868
69- 代码支持:Python3
69+ 代码支持:C++,Java,Python3
70+
71+
72+ C++ Code:
73+
74+ ``` c++
75+ class Solution {
76+ public:
77+ int lengthOfLongestSubstring(string s) {
78+
79+ int ans = 0, start = 0;
80+ int n = s.length();
81+ //
82+ map<char, int> mp;
83+
84+ for(int i=0;i<n;i++)
85+ {
86+ char alpha = s[i];
87+ if(mp.count(alpha))
88+ {
89+ start = max(start, mp[alpha]+1);
90+ }
91+ ans = max(ans, i-start+1 );
92+ // 字符位置
93+ mp[alpha] = i;
94+ }
95+
96+ return ans;
97+ }
98+ };
99+ ```
100+
101+
102+ Java Code:
103+
104+ ``` java
105+ class Solution {
106+ public int lengthOfLongestSubstring (String s ) {
107+ int ans = 0 , start = 0 ;
108+ int n = s. length();
109+ //
110+ Map<Character , Integer > map = new HashMap<> ();
111+
112+ for (int i= 0 ;i< n;i++ )
113+ {
114+ char alpha = s. charAt(i);
115+ if (map. containsKey(alpha))
116+ {
117+ start = Math . max(start, map. get(alpha)+ 1 );
118+ }
119+ ans = Math . max(ans, i- start+ 1 );
120+ // 字符位置
121+ map. put(alpha, i);
122+ }
123+
124+ return ans;
125+ }
126+ }
127+ ```
70128
71129Python3 Code:
72130
You can’t perform that action at this time.
0 commit comments