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? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。 示例 2: 输入: "bbbbb" 输出: 1 解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。 示例 3: 输入: "pwwkew" 输出: 3 解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。 请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
双指针法
/** * 双指针法 */ var lengthOfLongestSubstring = function(s) { //#1 设置最大字符串 | 可变字符串 let maxStr = ''; let str = '' //#2 遍历字符串 for (let i = 0; i < s.length; i++) { //#3 取字符,判断str是否存在 let chat = s[i]; let index = str.indexOf(chat); if (index === -1) { //#4 不存在,str相加与maxStr比较 str += chat; if (str.length > maxStr.length) maxStr = str; } else { //#5 存在,加上当前字符,然后截取重复字符之后的字符串 str += chat; str = str.slice(index + 1); } } return maxStr.length; };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
题目描述
算法
答案
The text was updated successfully, but these errors were encountered: