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.
Subscribe to see which companies asked this question
每读到一个字符,就检索这个字符上次出现的位置,如果这个位置比现在的非重复字符串的起点靠后,就把起点置在那个字符后面一位(因为只要你想要把现在的字符放在非重复串里,前一个字符就不能加进来)。
public class Solution {
public int lengthOfLongestSubstring(String s) {
int begin =0; int end =0;int max=0;
// boolean [] exist=new boolean [26];
int [] place = new int [200];
for(int i=0;i<200;i++){
// exist[i]=false;
place[i]=-1;
}
for(begin=0;begin<s.length();begin++){
if(place[(int)(s.charAt(begin))]>=end&&place[(int)(s.charAt(begin))]!=-1)end=place[(int)(s.charAt(begin))]+1;
place[(int)(s.charAt(begin))]=begin;
if((begin-end+1)>max)max=begin-end+1;
}return max;
}
}