代码拉取完成,页面将自动刷新
同步操作将从 doocs/leetcode 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
Given a string s
, find the length of the longest substring without repeating characters.
Example 1:
Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: s = "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1.
Example 3:
Input: s = "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
Example 4:
Input: s = "" Output: 0
Constraints:
0 <= s.length <= 5 * 104
s
consists of English letters, digits, symbols and spaces.class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
res, chars = 0, dict()
i = j = 0
while j < len(s):
if s[j] in chars:
i = max(i, chars[s[j]] + 1)
res = max(res, j - i + 1)
chars[s[j]] = j
j += 1
return res
class Solution {
public int lengthOfLongestSubstring(String s) {
int res = 0;
Map<Character, Integer> chars = new HashMap<>();
for (int i = 0, j = 0; j < s.length(); ++j) {
char c = s.charAt(j);
if (chars.containsKey(c)) {
i = Math.max(i, chars.get(c) + 1);
}
chars.put(c, j);
res = Math.max(res, j - i + 1);
}
return res;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。