From abd2e4f62b25a4240ab98b53fef951b6c1af9b67 Mon Sep 17 00:00:00 2001 From: zhaocchen Date: Thu, 8 Jul 2021 19:15:13 +0800 Subject: [PATCH] feat: add typescript solution to lc problem: No.0003.Longest Substring Without Repeating Characters --- .../README.md | 20 +++++++++++++++++++ .../README_EN.md | 20 +++++++++++++++++++ .../Solution.ts | 15 ++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.ts diff --git a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md index 95eb2bed2121d..57515fced0a38 100644 --- a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md +++ b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md @@ -106,6 +106,26 @@ class Solution { } ``` +### **TypeScript** + +```ts +function lengthOfLongestSubstring(s: string): number { + // 滑动窗口+哈希表 + let left = -1; + let maxLen = 0; + let hashTable = new Map(); + for (let right = 0; right < s.length; right++) { + let cur = s.charAt(right); + if (hashTable.has(cur)) { + left = Math.max(left, hashTable.get(cur)); + } + hashTable.set(cur, right); + maxLen = Math.max(maxLen, right - left); + } + return maxLen; + }; +``` + ### **Swift** ```swift diff --git a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md index f7847fe51eed8..d38e337d59973 100644 --- a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md +++ b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md @@ -88,6 +88,26 @@ class Solution { } ``` +### **TypeScript** + +```ts +function lengthOfLongestSubstring(s: string): number { + // 滑动窗口+哈希表 + let left = -1; + let maxLen = 0; + let hashTable = new Map(); + for (let right = 0; right < s.length; right++) { + let cur = s.charAt(right); + if (hashTable.has(cur)) { + left = Math.max(left, hashTable.get(cur)); + } + hashTable.set(cur, right); + maxLen = Math.max(maxLen, right - left); + } + return maxLen; + }; +``` + ### **Swift** ```swift diff --git a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.ts b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.ts new file mode 100644 index 0000000000000..e2bc91c9337ec --- /dev/null +++ b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.ts @@ -0,0 +1,15 @@ +function lengthOfLongestSubstring(s: string): number { + // 滑动窗口+哈希表 + let left = -1; + let maxLen = 0; + let hashTable = new Map(); + for (let right = 0; right < s.length; right++) { + let cur = s.charAt(right); + if (hashTable.has(cur)) { + left = Math.max(left, hashTable.get(cur)); + } + hashTable.set(cur, right); + maxLen = Math.max(maxLen, right - left); + } + return maxLen; + }; \ No newline at end of file