【LeetCode 0003】【滑动窗口】无重复字符的最长子串

  1. Longest Substring Without Repeating Characters

Given a string s, find the length of the longest substring 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
javascript 复制代码
/**
 * @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
};
相关推荐
C7211BA16 分钟前
使用knn算法对iris数据集进行分类
算法·分类·数据挖掘
Tisfy18 分钟前
LeetCode 2398.预算内的最多机器人数目:滑动窗口+单调队列——思路清晰的一篇题解
算法·leetcode·机器人·题解·滑动窗口
.普通人20 分钟前
c语言--力扣简单题目(回文链表)讲解
c语言·leetcode·链表
程序猿练习生22 分钟前
C++速通LeetCode简单第18题-杨辉三角(全网唯一递归法)
c++·算法·leetcode
Huazzi.25 分钟前
算法题解:斐波那契数列(C语言)
c语言·开发语言·算法
汉字萌萌哒26 分钟前
【2022 CCF 非专业级别软件能力认证第一轮(CSP-J1)入门级 C++语言试题及解析】
数据结构·c++·算法
2301_8071805427 分钟前
icpc江西:L. campus(dij最短路)
算法
th新港28 分钟前
CCF201909_1
数据结构·c++·算法·ccf
DdddJMs__13529 分钟前
C语言 | Leetcode C语言题解之题409题最长回文串
c语言·leetcode·题解
Dola_Pan29 分钟前
字符串的KMP算法详解及C/C++代码实现
算法