Giter Site home page Giter Site logo

Comments (5)

xiongcaihu avatar xiongcaihu commented on May 4, 2024
function hehe(str) {
    if (str.length == 0) return '';
    var map = {};
    var maxLen = 1;
    var maxChar = str[0];
    for (var i = 0; i < str.length; i++) {
        var char = str[i];
        if (map[char] != null) {
            if (i - map[char] + 1 > maxLen) {
                maxLen = i - map[char] + 1;
                maxChar = char;
            }
        } else {
            map[char] = i;
        }
    }

    return str.slice(map[maxChar], map[maxChar] + maxLen);
}

from leetcode.

azl397985856 avatar azl397985856 commented on May 4, 2024
function hehe(str) {
    if (str.length == 0) return '';
    var map = {};
    var maxLen = 1;
    var maxChar = str[0];
    for (var i = 0; i < str.length; i++) {
        var char = str[i];
        if (map[char] != null) {
            if (i - map[char] + 1 > maxLen) {
                maxLen = i - map[char] + 1;
                maxChar = char;
            }
        } else {
            map[char] = i;
        }
    }

    return str.slice(map[maxChar], map[maxChar] + maxLen);
}

每次看你代码我都想哭T﹏T😭

from leetcode.

azl397985856 avatar azl397985856 commented on May 4, 2024

基本思路:

用一个hashmap去存储每一个字符第一次出现位置,遍历过程中如果当前字符出现过,算出当前长度 判断是否最长

代码:

function LES(str) {
    const mapper = {};
    let maxLen = 0;
    let start = 0;

    for(let i = 0; i < str.length; i++) {
        const c = str[i];
        // 存储每一个字符第一次出现位置
        if (mapper[c] === void 0) {
            mapper[c] = i;
        } else if (i - mapper[c] > maxLen) {
            // 更新最大值
            start = mapper[c];
            maxLen = i - mapper[c];
        }
    }

    return str.slice(start, start + maxLen + 1)
}

from leetcode.

jinwei14 avatar jinwei14 commented on May 4, 2024

这个思路有问题吧 ?可以过么

from leetcode.

azl397985856 avatar azl397985856 commented on May 4, 2024

这个思路有问题吧 ?可以过么

说出你的想法

from leetcode.

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.