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

示例 1:

  1. 输入: s = "abcabcbb"
  2. 输出: 3
  3. 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3

示例 2:

  1. 输入: s = "bbbbb"
  2. 输出: 1
  3. 解释: 因为无重复字符的最长子串是 "b",所以其长度为 1

示例 3:

  1. 输入: s = "pwwkew"
  2. 输出: 3
  3. 解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3
  4. 请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。

示例 4:

  1. 输入: s = ""
  2. 输出: 0

解析

第一种 枚举所有的情况

  1. function lengthOfLongestSubstring(s){
  2. const len = s.length
  3. let queue = []
  4. let max = 0
  5. for(let i = 0; i<len; i++){
  6. queue = []
  7. for(let j = i; j<len; j++){
  8. if(!queue.includes(s[j])){
  9. queue.push(s[j])
  10. if(queue.length>max){
  11. max = queue.length
  12. }
  13. }else{
  14. break
  15. }
  16. }
  17. }
  18. return max
  19. }

第二种方法 是滑动窗口的方法

  1. function lengthOfLongestSubstring(s){
  2. const len = s.length
  3. let queue = []
  4. let res = []
  5. let max = 0
  6. let index = 0
  7. while( index < len ){
  8. const subIndex = queue.indexOf(s[index])
  9. queue.push(s[index])
  10. if(subIndex==-1){
  11. if(queue.length>max){
  12. max = queue.length
  13. res = queue.slice()
  14. }else{
  15. queue = queue.slice(index)
  16. }
  17. }
  18. }
  19. return max
  20. }

第三种方法 应该是散列法可是我不会 哈哈哈