s
,请你找出其中不含有重复字符的 最长子串 的长度。0 <= s.length <= 5 * 104
s
由英文字母、数字、符号和空格组成
主要思路是使用滑动窗口,分别使用start、end 指针,前提是保证两个指针单调递增,剩余逻辑就是处理指针移动的逻辑。
java代码如下:
public int lengthOfLongestSubstring(String s) {
int start = 0;
int end = 0;
int res = 0;
//记录出现的位置
int[] array = new int[128];
while (end < s.length()) {
int index = s.charAt(end);
//移动start指针
start = Math.max(start, array[index]);
//移动end指针
end += 1;
//计算最大窗口
res = Math.max(res, end - start);
//记录出现字符位置,重置start指针准备
array[index] = end;
}
return res;
}
Leetcode Longest Substring Without Repeating Characters & 无重复字符的最长子串 解题报告
原文:https://www.cnblogs.com/worldline/p/15165865.html