LeetCode--求最长不重复子字符串
作者:
zsliger | 来源:发表于
2016-05-26 11:20 被阅读115次测试用例:
{"abcabcbb",3}{"bbbbb",1}{"pwwkew",4} {"dvdf",3}
算法如下
public int lenghOfLongestSubString(String src) {
Map<Character, Integer> map = new HashMap<>();
int left=0;//子字符串的开始位置
int max=0;//子字符串的最大长度
char[] srcArray = src.toCharArray();
for (int i=0;i<srcArray.length;i++) {
int j;
if (map.containsKey(srcArray[i])) {
j = map.get(srcArray[i]);
if (left <= j) {
left = j + 1;
}
}
map.put(srcArray[i], i);
max = Math.max(i - left + 1, max);
}
return max;
}
本文标题:LeetCode--求最长不重复子字符串
本文链接:https://www.haomeiwen.com/subject/pkfxdttx.html
网友评论