Skip to content
New issue

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

无重复字符的最长子串 #357

Open
lzxjack opened this issue Jan 11, 2023 · 3 comments
Open

无重复字符的最长子串 #357

lzxjack opened this issue Jan 11, 2023 · 3 comments

Comments

@lzxjack
Copy link
Contributor

lzxjack commented Jan 11, 2023

No description provided.

@1584965284
Copy link

1584965284 commented Jan 23, 2023

function lengthOfLongestSubstring(s: string): number {
  let i=0,j=0,max=0;
  let set:Set<string>=new Set();
  while(j<s.length){
    if(!set.has(s[j])){
      set.add(s[j]);
      if(set.size>max)max=set.size;
      ++j;
    }else{
      while(set.has(s[j])){
        set.delete(s[i++]);
      }
      set.add(s[j]);
      ++j;
    }
  }
  return max;
};

@OLDLAI
Copy link

OLDLAI commented Mar 25, 2023

var lengthOfLongestSubstring = function(s) {

    let map = new Map(); let max = 0;

    for(let i=0,j=0; j<s.length ; j++)

    {

        if(map.has(s[j])){

            i = Math.max(map.get(s[j])+1,i) 

        }

        max = Math.max(max,j-i+1)//关键,j-i+1

        map.set(s[j],j)

    }

    return max;

};

@dizao006
Copy link

滑动窗口
var lengthOfLongestSubstring = function (s) {
    let map = new Set()
    let left = 0
    let ans = 0
    for (let right = 0; right < s.length; right++) {
        while (map.has(s[right])) {
            map.delete(s[left]) //注意,这里是删除s[left]
            left++
        }
        map.add(s[right])
        ans = Math.max(ans, right - left + 1)
    }
    return ans
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants