3.无重复字符的最长子串
1
public class Solution {
2
public int lengthOfLongestSubstring(String s) {
3
int n = s.length();
4
int ans = 0;
5
//列出所有的子串(i,j)并判断是否相等
6
for (int i = 0; i < n; i++)
7
for (int j = i + 1; j <= n; j++)
8
if (allUnique(s, i, j)) ans = Math.max(ans, j - i);
9
return ans;
10
}
11
12
public boolean allUnique(String s, int start, int end) {
13
Set<Character> set = new HashSet<>();
14
for (int i = start; i < end; i++) {
15
Character ch = s.charAt(i);
16
if (set.contains(ch)) return false;
17
set.add(ch);
18
}
19
return true;
20
}
21
}
Copied!
最近更新 28d ago
复制链接