leetcode Longest Substring Without Repeating Characters 难度系数3 3.2
2014-01-28 11:55
543 查看
Question:
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.
public class Solution {
public int lengthOfLongestSubstring(String s) {
if (s == null || s.length() == 0) {
return 0;
}
int max = 0;
int start = 0;
int[] lastindex = new int[26];
Arrays.fill(lastindex, -1);
for (int i = 0; i < s.length(); i++) {
if (lastindex[s.charAt(i) - 'a'] >= start) {
max = Math.max(i - start, max);
start = lastindex[s.charAt(i) - 'a'] + 1;
}
lastindex[s.charAt(i) - 'a'] = i;
}
return Math.max(s.length()-start, 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.
public class Solution {
public int lengthOfLongestSubstring(String s) {
if (s == null || s.length() == 0) {
return 0;
}
int max = 0;
int start = 0;
int[] lastindex = new int[26];
Arrays.fill(lastindex, -1);
for (int i = 0; i < s.length(); i++) {
if (lastindex[s.charAt(i) - 'a'] >= start) {
max = Math.max(i - start, max);
start = lastindex[s.charAt(i) - 'a'] + 1;
}
lastindex[s.charAt(i) - 'a'] = i;
}
return Math.max(s.length()-start, max);
}
}
相关文章推荐
- leetcode Longest Substring Without Repeating Characters解题记录
- 【leetcode】Longest Substring Without Repeating Characters
- LeetCode_003 Longest Substring Without Repeating Characters(greedy)
- [Leetcode][python]Longest Substring Without Repeating Characters
- [LeetCode]Longest Substring Without Repeating Characters
- [leetcode][3]Longest Substring Without Repeating Characters
- 【leetcode 002】Longest Substring Without Repeating Characters || Median of Two Sorted Arrays
- LeetCode Longest Substring Without Repeating Characters
- LeetCode_Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters--LeetCode
- LeetCode -- Longest Substring Without Repeating Characters
- leetcode 3 golang实现 Longest Substring Without Repeating Characters
- leetcode-3 Longest Substring Without Repeating Characters
- [LeetCode]Longest Substring Without Repeating Characters
- LeetCode Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters(leetcode)
- [LeetCode]003-Longest Substring Without Repeating Characters
- [*leetcode 3] Longest Substring Without Repeating Characters
- leetcode3 Longest Substring Without Repeating Characters
- Leetcode3:Longest Substring Without Repeating Characters@Python