Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a9e84ff

Browse files
feat: add typescript solution to lc problem: No.0003.Longest Substring Without Repeating Characters (doocs#505)
1 parent e934412 commit a9e84ff

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

‎solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,26 @@ class Solution {
106106
}
107107
```
108108

109+
### **TypeScript**
110+
111+
```ts
112+
function lengthOfLongestSubstring(s: string): number {
113+
// 滑动窗口+哈希表
114+
let left = -1;
115+
let maxLen = 0;
116+
let hashTable = new Map();
117+
for (let right = 0; right < s.length; right++) {
118+
let cur = s.charAt(right);
119+
if (hashTable.has(cur)) {
120+
left = Math.max(left, hashTable.get(cur));
121+
}
122+
hashTable.set(cur, right);
123+
maxLen = Math.max(maxLen, right - left);
124+
}
125+
return maxLen;
126+
};
127+
```
128+
109129
### **Swift**
110130

111131
```swift

‎solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,26 @@ class Solution {
8888
}
8989
```
9090

91+
### **TypeScript**
92+
93+
```ts
94+
function lengthOfLongestSubstring(s: string): number {
95+
// 滑动窗口+哈希表
96+
let left = -1;
97+
let maxLen = 0;
98+
let hashTable = new Map();
99+
for (let right = 0; right < s.length; right++) {
100+
let cur = s.charAt(right);
101+
if (hashTable.has(cur)) {
102+
left = Math.max(left, hashTable.get(cur));
103+
}
104+
hashTable.set(cur, right);
105+
maxLen = Math.max(maxLen, right - left);
106+
}
107+
return maxLen;
108+
};
109+
```
110+
91111
### **Swift**
92112

93113
```swift
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function lengthOfLongestSubstring(s: string): number {
2+
// 滑动窗口+哈希表
3+
let left = -1;
4+
let maxLen = 0;
5+
let hashTable = new Map();
6+
for (let right = 0; right < s.length; right++) {
7+
let cur = s.charAt(right);
8+
if (hashTable.has(cur)) {
9+
left = Math.max(left, hashTable.get(cur));
10+
}
11+
hashTable.set(cur, right);
12+
maxLen = Math.max(maxLen, right - left);
13+
}
14+
return maxLen;
15+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /