本地测试通过但是提交出错
public class wangba {
public static void main(String[] args)
{
String string = "abb";
System.out.println(lengthOfLongestSubstring(string));
}
public int lengthOfLongestSubstring(String s) {
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
int max = 0;
int j = 0;
if (s == null || s.isEmpty()) {
return 0;
}
for (int i = 0; i <s.length(); i++) {
if (map.containsKey(s.charAt(i))) {
map.put(s.charAt(i), i + 1);
map.remove(s.charAt(j));
j++;
} else {
map.put(s.charAt(i), i + 1);
max = Math.max(max, map.size());
}
}
return max;
}
运行结果:2
在leetcode上运行结果为1