Giter Club home page Giter Club logo

Comments (3)

wingmeng avatar wingmeng commented on July 24, 2024 4

这题还是有点难度的,一开始是打算用“双指针”来解的,后来感觉用正则表达式更优雅。

const noRepeatSubstrLen = str => {
  let len = str ? str.length : 0;

  // 空字符和1个字符的情况
  if (len < 2) {
    return len;
  }

  let arr = [
    ...new Set(  // 去重
      str.split(/(.+?)(\1)/g)  // 贪婪模式匹配所有连续字符
    )
  ].map(s => s.length);  // 去重后统计每项的字符串长度
  
  return Math.max.apply(null, arr);
}

console.log(noRepeatSubstrLen(''));  // 0
console.log(noRepeatSubstrLen('a'));  // 1
console.log(noRepeatSubstrLen('ab'));  // 2
console.log(noRepeatSubstrLen('aa'));  // 1
console.log(noRepeatSubstrLen('abcabcbb'));  // 3
console.log(noRepeatSubstrLen('bbbbb'));  // 1
console.log(noRepeatSubstrLen('pwwkew'));  // 3
console.log(noRepeatSubstrLen('qqwewert'));  // 2

from fe-practice-hard.

cnyballk avatar cnyballk commented on July 24, 2024 3
const get666Length = s => {
  if (s.length < 2) return s.length;
  let index,
    i = 1,
    discardLength = 0,
    max = 0;
  for (; i < s.length; i++) {
    index = s.lastIndexOf(s[i], i - 1);
    if (index !== -1) {
      max = Math.max(max, i - discardLength);
      discardLength = Math.max(discardLength, index + 1);
    }
  }
  return Math.max(max, i - discardLength);
};

from fe-practice-hard.

liwenkang avatar liwenkang commented on July 24, 2024

我的怕是会超时⊙﹏⊙

// 求无重复字符的最长子串
const getMaxCount = (string) => {
    // 计算最长不重复连续字符的个数
    const getCount = (string, lastStr = '') => {
        lastStr = lastStr ? lastStr : string[0];
        for (let i = 1; i < string.length; i++) {
            if (lastStr.includes(string[i])) {
                // 有重复的,直接输出
                return lastStr.length;
            } else {
                // 没有重复的,继续循环
                lastStr += string[i];
                getCount(string, lastStr);
            }
        }
        return lastStr.length;
    };

    let max = 0;
    for (let i = 0; i < string.length; i++) {
        let count = getCount(string.slice(i));
        if (count > max) {
            max = count;
        }
    }
    return max;
};

from fe-practice-hard.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.