Algorithm
无重复字符的最长子串(3)
解法1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| import java.util.HashSet; import java.util.Set;
class Solution { public int lengthOfLongestSubstring(String s) { int result = 0; int l = s.length(); for (int i = 0; i < l; i++) { for (int j = i + 1; j <= l; j++) { if (allUnique(s, i, j)) { result = Math.max(result, j - i); } } } return result; }
private boolean allUnique(String s, int start, int end) { Set<Character> set = new HashSet<>(); for (int i = start; i < end; i++) { Character c = s.charAt(i); if (!set.contains(c)) { set.add(c); } else { return false; } } return true; } }
|
暴力破解法没啥好说的, 直接超时了, 显然是不可行的.
时间复杂度:O(n^3)
解法2: