From 59ac9f8d47fcbae3538d8ba6b616013133eb497f Mon Sep 17 00:00:00 2001 From: Qiming Xu <458173774@qq.com> Date: Fri, 1 Mar 2024 00:31:00 +0000 Subject: [PATCH] leetcode: finished #3 --- content/leetcode/2024/3.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 content/leetcode/2024/3.md diff --git a/content/leetcode/2024/3.md b/content/leetcode/2024/3.md new file mode 100644 index 00000000..cfe61958 --- /dev/null +++ b/content/leetcode/2024/3.md @@ -0,0 +1,36 @@ +--- +title: 2024.3 +draft: false +--- + +# 2024.3.1 + +```typescript +/* + * @lc app=leetcode.cn id=3 lang=typescript + * + * [3] 无重复字符的最长子串 + */ + +// @lc code=start +function lengthOfLongestSubstring(s: string): number { + let charSet = new Set(); + let [left, maxLength, currentLength] = [0, 0, 0]; + for (let i = 0; i < s.length; i += 1) { + while (charSet.has(s[i])) { + charSet.delete(s[left++]); + currentLength -= 1; + } + charSet.add(s[i]); + currentLength += 1; + if (currentLength > maxLength) maxLength = currentLength; + } + return maxLength; +} +// @lc code=end + +console.log(lengthOfLongestSubstring("abcabcbb")); +console.log(lengthOfLongestSubstring("bbbbb")); +console.log(lengthOfLongestSubstring("pwwkew")); +console.log(lengthOfLongestSubstring("aab")); +```