Leetcode - Longest Substring Without Repeating Characters

来源:互联网 发布:尼康d610调焦软件 编辑:程序博客网 时间:2024/05/16 01:04

public class Solution {
 public int lengthOfLongestSubstring(String s) {
HashMap<Character, Integer> hm = new HashMap<Character, Integer>();
int Len=0, max = 0;
if(s.length()<1)
 return max;
 
hm.put(s.charAt(0),0);

max = Len = 1;


for(int i=1; i<s.length(); i++){
char c = s.charAt(i);
if(!hm.containsKey(c) || i>hm.get(c)+Len){
Len ++;
}
else {
Len = i-hm.get(c);
}
hm.put(c, i);

if(Len > max)
max = Len;
}
return max;
}
}

---------------------

HINT:

1. Hashmap 的灵活运用,然后1.思路清晰2.表达完整

2. 题目要了解,这里string里面都是字母,没有特殊字符。。。。。。。这个留作my 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.

0 0
原创粉丝点击