给定一个字符串
s
,请你找出其中不含有重复字符的 最长子串 的长度。
这里我用的队列解决:
class Solution {
public int lengthOfLongestSubstring(String s) {
LinkedList<Character> queue = new LinkedList<>();
int maxLen = 0;
for (int i = 0; i < s.length(); i++) {
Character c = s.charAt(i);
if (!queue.contains(c)) {
queue.add(c);
} else {
while (queue.getFirst() != c) {
queue.remove();
}
queue.remove();
queue.add(c);
}
maxLen = Math.max(maxLen, queue.size());
}
return maxLen;
}
}