题目:
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
示例:

代码:
- 解法
class Solution {
public int lengthOfLongestSubstring(String s) {
int n = s.length(), ans = 0;
int[] index = new int[128]; // current index of character
// try to extend the range [i, j]
for (int j = 0, i = 0; j < n; j++) {
i = Math.max(index[s.charAt(j)], i);
ans = Math.max(ans, j - i + 1);
index[s.charAt(j)] = j + 1;
}
return ans;
}
}
- 别人的代码
class Solution {
public int lengthOfLongestSubstring(String s) {
int n = s.length(), ans = 0;
int[] index = new int[128]; // current index of character
// try to extend the range [i, j]
for (int j = 0, i = 0; j < n; j++) {
i = Math.max(index[s.charAt(j)], i);
ans = Math.max(ans, j - i + 1);
index[s.charAt(j)] = j + 1;
}
return ans;
}
}
博客围绕给定字符串,要求找出其中不含有重复字符的最长子串的长度,并给出了解法及别人的代码示例,主要涉及字符串处理和算法求解。
385

被折叠的 条评论
为什么被折叠?



