【LeetCode 0003】【滑动窗口】无重复字符的最长子串
创始人
2024-11-19 05:37:31
  1. Longest Substring Without Repeating Characters

Given a string s, find the length of the longestsubstring without repeating characters.

Example 1:

**Input:** s = "abcabcbb" **Output:** 3 **Explanation:** The answer is "abc", with the length of 3. 

Example 2:

**Input:** s = "bbbbb" **Output:** 1 **Explanation:** The answer is "b", with the length of 1. 

Example 3:

**Input:** s = "pwwkew" **Output:** 3 **Explanation:** The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring. 

Constraints:

  • 0 <= s.length <= 5 * 10^4
  • s consists of English letters, digits, symbols and spaces.
JavaScript Solution
/**  * @param {string} s  * @return {number}  */ var lengthOfLongestSubstring = function(s) {     let ans = 0     if('' === s){         return ans     }        let [left,right] = [0,-1]     // matain the mapping from character to isPresent Flag      let flags = {}     while(left < s.length){         // mark all different elements as 1s         if(( right+1 ) < s.length && !flags[s[right+1]] ){             flags[s[ right+1 ]] = 1             right++             ans = Math.max(ans,right-left+1)         }else{             // sliding leftmost 1s to 0s             flags[s[left]] = 0             left++         }      }     return ans }; 

相关内容

热门资讯

刚刚,Claude最新功能泄露... 新智元报道 编辑:定慧 大卫 【新智元导读】2026年5月4日,testingcatalog在An...
高分辨大宽带集成光子光谱仪成功... 麦姆斯咨询获悉,近日,中国科学院南京天文光学技术研究所天文光子学团队在面向天文观测的高分辨大宽带集成...
性价比高又稳定的云手机哪个好?... 作为搬了4年砖、踩过无数云手机坑的老玩家,今天直接给你们唠唠性价比高又稳定的云手机选法,全是实战干货...
以灵石破局,万物云参编国内首部... 4月23日,由低碳智慧建筑产业技术创新战略联盟与北京清华同衡规划设计研究院有限公司主办、万物云作为协...
专访 | CLA成功反哺全球 ... 2026年,是奔驰诞生的140周年,也是奔驰进入中国内地市场的20周年。 140年间,从第一款汽车问...