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

3. 无重复字符的最长子串 #21

Open
zpc7 opened this issue Aug 6, 2022 · 0 comments
Open

3. 无重复字符的最长子串 #21

zpc7 opened this issue Aug 6, 2022 · 0 comments
Labels
中等 LeetCode 难度定级

Comments

@zpc7
Copy link
Owner

zpc7 commented Aug 6, 2022

3. 无重复字符的最长子串

function lengthOfLongestSubstring(s: string): number {
    if (s === '') return 0;

    const strArr = s.split('');
    const stack = [];

    for (let i = 0; i < strArr.length; i++) {
        if (i === 0) {
            stack.push(strArr[i]);
            continue;
        }
        const index = stack[i - 1].indexOf(strArr[i]);
        if (index === -1) {
            stack.push(stack[i - 1] + strArr[i])
        } else {
            stack.push(stack[i - 1].slice(index + 1) + strArr[i])
        }

    }
    return Math.max(...stack.map(i => i.length))
};
@zpc7 zpc7 added 中等 LeetCode 难度定级 JS labels Aug 6, 2022
@zpc7 zpc7 removed the JS label Aug 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
中等 LeetCode 难度定级
Projects
None yet
Development

No branches or pull requests

1 participant