码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode 3 Longest Substring Without Repeating Characters

时间:2015-01-04 11:03:57      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

public class Solution {
public int lengthOfLongestSubstring(String s) {
int maxLen = 0;
int maxLenTmp = 0;
Map<Character, Integer> map = new HashMap<Character, Integer>(
s.length() / 2);
char[] ss = s.toCharArray();

int times = ss.length;
int start = 0;
for (int i = 0; i < times; i++) {

char a = ss[i];
if (map.containsKey(a)) {
if (maxLenTmp > maxLen) {
maxLen = maxLenTmp;
}

int end = map.get(a);
for (int j = start; j <= end; j++) {

map.remove(ss[j]);
maxLenTmp --;
}
i --;
start = end + 1;

continue;
} else {
map.put(a, i);
maxLenTmp++;
}

if (i == times - 1) {
if (maxLenTmp > maxLen) {
maxLen = maxLenTmp;
}
}


}
return maxLen;
}
}

技术分享

leetcode 3 Longest Substring Without Repeating Characters

标签:

原文地址:http://www.cnblogs.com/aDreamer/p/4200436.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!