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.
在做这个题目的时候理解错了,以为是在一个字符串里面找出最长的不重复的字符串,然而并不是,看discuss看出了特别好的的答案,
public int lengthOfLongestSubstring(String s) {
LinkedList<Character> seq = new LinkedList<Character>();
int max = 0;
for(int i=0;i<s.length();i++){
char a = s.charAt(i);
if(seq.indexOf(a)>=0){
int num = seq.indexOf(a)+1;
while(num-->0)
seq.removeFirst();
}
seq.add(a);
if(max<seq.size())
max = seq.size();
}
return max;
}
其中在JAVA里面 likedList 和 Arraylist中的差别
Arraylist 像数组一样但是优于数组可以动态存储
likedList 向链表一样 需要移动指针