We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
示例 1:
输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
示例 2:
输入: "bbbbb" 输出: 1 解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
示例 3:
输入: "pwwkew" 输出: 3 解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
这是一道动态规划题目 1.声明两个变量, currentString: 当前无重复字符的子串,max,无重复字符的最大子串长度 2.判断当前的最长串中是否有该字母s[i],
/** * @param {string} s * @return {number} */ var lengthOfLongestSubstring = function(s) { let currentString = ''; let max = 0; for(let i = 0; i < s.length; i++) { const index = currentString.indexOf(s[i]); // 判断当前的最长串中是否有该字母 if( index < 0) { // 如果没有,则加上s[i] currentString += s[i]; } else { // 如果有,则需要从重复的位置断开,开始新的计算 currentString = currentString.substring(index + 1) + s[i]; } max = Math.max(max, currentString.length); } return max; };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
题目描述
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
示例 1:
示例 2:
示例 3:
解题思路
这是一道动态规划题目
1.声明两个变量, currentString: 当前无重复字符的子串,max,无重复字符的最大子串长度
2.判断当前的最长串中是否有该字母s[i],
解题方案
The text was updated successfully, but these errors were encountered: