Skip to content

Commit

Permalink
leetcode: finished #3
Browse files Browse the repository at this point in the history
  • Loading branch information
xqm32 committed Mar 1, 2024
1 parent 6e686bc commit 59ac9f8
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions content/leetcode/2024/3.md
Original file line number Diff line number Diff line change
@@ -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<string>();
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"));
```

0 comments on commit 59ac9f8

Please sign in to comment.