您的位置:首页 > 职场人生

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);

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java leetcode 面试