LeetCode 无重复字符的最长子串算法题解 All In One
LeetCode 无重复字符的最长子串算法题解 All In One
js / 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
*
*/
export {
lengthOfLongestSubstring,
};
const log = console.log;
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;
}
function getMaxSubStr(str: string): string {
let result = ``;
let maxStr = ``;
let maxLen = 0;
for(const c of str) {
let index = maxStr.indexOf(c);
if(index > -1) {
// slice + 1,截取当前字符后面的字符串
maxStr = maxStr.slice(index + 1);
}
maxStr += c;
if(maxLen < maxStr.length) {
maxLen = maxStr.length;
result = maxStr;
}
}
return result;
}
function getMaxSubStrObj(str: string): Object {
let result = ``;
let maxStr = ``;
let maxLen = 0;
for(const c of str) {
let index = maxStr.indexOf(c);
if(index > -1) {
// slice + 1,截取当前字符后面的字符串
maxStr = maxStr.slice(index + 1);
}
maxStr += c;
if(maxLen < maxStr.length) {
maxLen = maxStr.length;
result = maxStr;
}
}
return {
maxStr: result,
maxLen
};
}
/*
"abcabcbb"
"bbbbb"
"pwwkew"
*/
// 测试用例 test cases
const testCases = [
{
input: "abcabcbb",
result: 3,
desc: 'value equal to 3',
},
{
input: "bbbbb",
result: 1,
desc: 'value equal to 1',
},
{
input: "pwwkew",
result: 3,
desc: 'value equal to 3',
},
];
for (const [i, testCase] of testCases.entries()) {
const result = lengthOfLongestSubstring(testCase.input);
log(`test case ${i} result: `, result === testCase.result ? `✅ passed` : `❌ failed`, result);
// log(`test case i result: `, result === testCase.result ? `passed ✅` : `failed ❌`, result);
// log(`test case i =`, testCase);
}
/*
$ npx ts-node ./003\ longest-substring-without-repeating-characters.ts
test case 0 result: ✅ passed 3
test case 1 result: ✅ passed 1
test case 2 result: ✅ passed 3
*/
https://leetcode.com/problems/longest-substring-without-repeating-characters/
https://leetcode.cn/problems/longest-substring-without-repeating-characters/
LeetCode 题解 / LeetCode Solutions
https://www.youtube.com/channel/UCftIXZeipv4MTVwmfFphtYw/videos
YouTube & LeetCode 力扣官方算法题解视频列表
https://www.youtube.com/playlist?list=PLamwFu9yMruCBtS2tHUD77oI_Wsce-syE
https://github.com/xgqfrms/leetcode/issues/14
https://www.youtube.com/results?search_query=+Leetcode+3
类似问题
LeetCode
https://leetcode.com/problems//
refs
https://www.cnblogs.com/xgqfrms/p/13649609.html
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/16746760.html
未经授权禁止转载,违者必究!