xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

LeetCode 无重复字符的最长子串 All In One

LeetCode 无重复字符的最长子串 All In One

LeetCode 算法刷题

Longest Substring Without Repeating Characters

无重复字符的最长子串

https://leetcode.com/problems/longest-substring-without-repeating-characters/

https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/



https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/solution/wu-zhong-fu-zi-fu-de-zui-chang-zi-chuan-by-leetc-2/

暴力解法 👎

  1. 生成所有子字符串
  2. 遍历找到重复的最大值
/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLongestSubstring = function(str) {
  let len = str.length;
  let maxs = [0]
  for(let i = 0; i < len; i++) {
    let max = 0;
    let arr = [];
    let temp = str.slice(i);
    let stop = false;
    for(let item of temp) {
      if(!arr.includes(item) && !stop) {
         arr.push(item);
         max += 1;
       } else {
         // break;
         stop = true;
       }
    }
    maxs.push(max);
  }
  return Math.max(...maxs);
};

/*

https://leetcode-cn.com/submissions/detail/106829316/testcase/

超出时间限制


lengthOfLongestSubstring("abcabcbb");
lengthOfLongestSubstring("bbbbb");
lengthOfLongestSubstring("pwwkew");


*/

超出时间限制 bug ❌

986 / 987 个通过测试用例
状态:超出时间限制

滑动窗口 👍

Sliding Window



时间复杂度:O(N), 其中 NN 是字符串的长度

空间复杂度:O(1), 

```ts
"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2022-09-30
 * @modified
 *
 * @description 3. 无重复字符的最长子串
 * @description 3. Longest Substring Without Repeating Characters
 * @difficulty Medium
 * @ime_complexity O(n)
 * @space_complexity O(1)
 * @augments
 * @example
 * @link https://leetcode.com/problems/longest-substring-without-repeating-characters/
 * @link https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
 * @solutions
 *
 * @best_solutions
 *
 */


function lengthOfLongestSubstring(str: string): number {
  let maxStr = ``;
  let maxLen = 0;
  for(const c of str) {
    let index = maxStr.indexOf(c);
    if(index > -1) {
      // slice + 1,截取当前字符后面的字符串
      // slice +1 从下一个字符开始截取
      maxStr = maxStr.slice(index + 1);
    }
    maxStr += c;
    if(maxLen < maxStr.length) {
      // 更新最长字符串
      maxLen = maxStr.length;
    }
    // maxLen = Math.max(maxLen, maxStr.length);
  }
  return maxLen;
}

## 滑动窗口优化 👍

> Sliding Window Optimized


```js
/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLongestSubstring = function(strs) {
  let maxStr = '';
  let maxLen = 0;
  for(let str of strs) {
    let index = maxStr.indexOf(str);
    if(index > -1) {
       // 如果存在,截取head
       maxStr = maxStr.substr(index + 1);
    }
    // 追加 str
    maxStr += str;
    // 计算 max (防止截取到最后,maxLen > maxStr.length)
    maxLen = Math.max(maxLen, maxStr.length)
  }
  return maxLen;
};

https://leetcode.com/problems/longest-substring-without-repeating-characters/submissions/

https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/solution/wu-zhong-fu-zi-fu-de-zui-chang-zi-chuan-by-leetc-2/

solutions

https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/?currentPage=1&orderBy=most_votes&query=&tag=javascript

/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLongestSubstring = function(s) {
  let sLen = s.length,
    maxLen = 0,
    maxStr = '',
    tmpStr,
    posIndex,
    i;
  for( i = 0 ; i < sLen; i++ ){
    tmpStr = s[i];
    posIndex = maxStr.indexOf(tmpStr);
    if(posIndex > -1){
      maxStr = maxStr.substring(posIndex + 1);
    }
    maxStr += tmpStr;
    maxLen = Math.max(maxLen, maxStr.length);
  }
  return maxLen;
};

/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLongestSubstring = function(s) {
    const map = {};
    let left = 0;
    return s.split('').reduce((max, v, i) => {
        left = map[v] >= left ? map[v] + 1 : left;
        map[v] = i;
        return Math.max(max, i - left + 1);
    }, 0);
}

字节跳动

https://leetcode-cn.com/explore/interview/card/bytedance/242/string/1012/

https://leetcode-cn.com/submissions/detail/106846699/

refs



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2020-09-11 08:22  xgqfrms  阅读(232)  评论(3编辑  收藏  举报