Leetcode 3. Longest Substring Without Repeating Characters
2017-01-15 03:46
399 查看
O(n)
public class Solution {
public int lengthOfLongestSubstring(String s) {
HashSet<Character> hs = new HashSet<Character>();
int max = 0, fast = 0, slow = 0;
while (fast < s.length()) {
if (!hs.add(s.charAt(fast))) {
hs.remove(s.charAt(slow));
slow++;
}
else {
// maintain the maximum length
max = Math.max(max, fast-slow+1);
fast++;
}
}
return max;
}
}
O(n^2)
// using a hashset to save the sunstring starting from 0, 1, ..., n
// keep recording the length for each substring
// if max < current length, set max = current length
public class Solution {
public int lengthOfLongestSubstring(String s) {
HashSet<Character> hs = new HashSet<Character>();
int max = 0;
// check the length for substring starts from i
for (int i=0; i<s.length(); i++) {
// tmp to save the current substring's length
int tmp = 0, j = i;
while (j < s.length()) {
// found a duplicate character
if (!hs.add(s.charAt(j)))
break;
tmp++;
j++;
}
// clear the hashset after each iteration
hs.clear();
// save the maximum length
max = Math.max(tmp, max);
}
return max;
}
}
public class Solution {
public int lengthOfLongestSubstring(String s) {
HashSet<Character> hs = new HashSet<Character>();
int max = 0, fast = 0, slow = 0;
while (fast < s.length()) {
if (!hs.add(s.charAt(fast))) {
hs.remove(s.charAt(slow));
slow++;
}
else {
// maintain the maximum length
max = Math.max(max, fast-slow+1);
fast++;
}
}
return max;
}
}
O(n^2)
// using a hashset to save the sunstring starting from 0, 1, ..., n
// keep recording the length for each substring
// if max < current length, set max = current length
public class Solution {
public int lengthOfLongestSubstring(String s) {
HashSet<Character> hs = new HashSet<Character>();
int max = 0;
// check the length for substring starts from i
for (int i=0; i<s.length(); i++) {
// tmp to save the current substring's length
int tmp = 0, j = i;
while (j < s.length()) {
// found a duplicate character
if (!hs.add(s.charAt(j)))
break;
tmp++;
j++;
}
// clear the hashset after each iteration
hs.clear();
// save the maximum length
max = Math.max(tmp, max);
}
return max;
}
}
相关文章推荐
- 【Leetcode】3. Longest Substring Without Repeating Characters
- [leetcode] 3. Longest Substring Without Repeating Characters
- LeetCode 3. Longest Substring Without Repeating Characters
- LeetCode-3. Longest Substring Without Repeating Characters
- 3. Longest Substring Without Repeating Characters Leetcode Python New season for 2016
- leetcode/algorithm 3. Longest Substring Without Repeating Characters
- leetcode : 3. Longest Substring Without Repeating Characters
- 【LeetCode】3. Longest Substring Without Repeating Characters
- [leetcode] 3. Longest Substring Without Repeating Characters
- LeetCode 3. Longest Substring Without Repeating Characters
- 【leetcode】3. Longest Substring Without Repeating Characters
- [Leetcode] 3. Longest Substring Without Repeating Characters
- [leetcode]3. Longest Substring Without Repeating Characters
- leetcode算法题之 3. Longest Substring Without Repeating Characters
- Leetcode 3. Longest Substring Without Repeating Characters
- leetcode 3. Longest Substring Without Repeating Characters
- Leetcode 3. Longest Substring Without Repeating Characters The Solution of Python and Javascript
- LeetCode 3. Longest Substring Without Repeating Characters
- Leetcode 3. Longest Substring Without Repeating Characters
- [LeetCode - 滑动窗口] 3. Longest Substring Without Repeating Characters