LeetCode | Longest Substring Without Repeating Characters
2014-04-08 14:52
363 查看
题目
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without
repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
分析
在遍历过程中,记录各个字符最后出现位置,如果子串出现重复字符,就移动子串的起始位置。
代码
import java.util.Arrays;
public class LongestSubstringWithoutRepeatingCharacters {
public int lengthOfLongestSubstring(String s) {
int max = 0;
int start = 0;
int end = 0;
int[] loc = new int[255];
Arrays.fill(loc, -1);
while (end < s.length()) {
int c = s.charAt(end);
start = loc[c] >= start ? loc[c] + 1 : start;
loc[c] = end;
max = Math.max(max, ++end - start);
}
return max;
}
}
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without
repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
分析
在遍历过程中,记录各个字符最后出现位置,如果子串出现重复字符,就移动子串的起始位置。
代码
import java.util.Arrays;
public class LongestSubstringWithoutRepeatingCharacters {
public int lengthOfLongestSubstring(String s) {
int max = 0;
int start = 0;
int end = 0;
int[] loc = new int[255];
Arrays.fill(loc, -1);
while (end < s.length()) {
int c = s.charAt(end);
start = loc[c] >= start ? loc[c] + 1 : start;
loc[c] = end;
max = Math.max(max, ++end - start);
}
return max;
}
}
相关文章推荐
- LeetCode:Longest Substring Without Repeating Characters(update)
- LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)
- leetcode 第三题Longest Substring Without Repeating Characters
- LeetCode学习笔记[3]:Longest Substring Without Repeating Characters
- Leetcode Longest Substring Without Repeating Characters
- LeetCode题解 || Longest Substring Without Repeating Characters (O(n)算法)问题
- 【LeetCode】Longest Substring Without Repeating Characters
- leetcode-Longest Substring Without Repeating Characters
- 【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters
- 【leetcode】Longest Substring Without Repeating Characters
- [leetcode] Longest Substring Without Repeating Characters
- LeetCode3 Longest Substring Without Repeating Characters
- LeetCode-Q3-Longest Substring Without Repeating Characters
- leetcode3 longest Substring Without Repeating Characters
- leetcode_Longest Substring Without Repeating Characters
- leetcode笔记:Longest Substring Without Repeating Characters
- leetcode——Longest Substring Without Repeating Characters
- LeetCode题库解答与分析——#3.无重复字符的最长子串LongestSubstringWithoutRepeatingCharacters
- LeetCode Longest Substring Without Repeating Characters
- LeetCode Longest Substring Without Repeating Characters