python
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
i,ans = 0,0
t = [0] * 128
for j, char in enumerate(s):
t[ord(char)] += 1
while(t[ord(char)] > 1):
t[ord(s[i])] -= 1
i += 1
ans = max(ans, j - i + 1)
return ans
java
class Solution {
public int lengthOfLongestSubstring(String s) {
int[] t = new int[128];
int ans = 0;
for(int i = 0, j = 0; j < s.length(); j++){
t[s.charAt(j)] ++;
while(t[s.charAt(j)] > 1){
t[s.charAt(i++)] --;
}
ans = Math.max(ans, j - i + 1);
}
return ans;
}
}
HashSet-JAVA/PYTHON
class Solution {
public int lengthOfLongestSubstring(String s) {
int res = 0;
Set<Character> t = new HashSet<>();
for(int i = 0,j = 0; j < s.length(); j++){
//如果【i,j】中有重复字符出现, 移除set中的元素且右移,直到使得【i,j】无重复为止
while (t.contains(s.charAt(j)))
t.remove(s.charAt(i++));
t.add(s.charAt(j));
res = Math.max(res, j - i+1);
}
return res;
}
}
// PYTHON
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
t = set()
i, res = 0, 0
for j, char in enumerate(s):
# 持续移除哈希表中s[i, ==char]
while(char in t):
t.remove(s[i])
i += 1
t.add(char)
res = max(res, j - i + 1)
return res
请问您是在pad上写,然后再通过电脑网页上传的吗
嗯嗯,用notability写完传上电脑里的~