From a92a4205205bb0816d373b702c4921332c185239 Mon Sep 17 00:00:00 2001 From: thinkasany <480968828@qq.com> Date: Sat, 19 Aug 2023 16:55:30 +0800 Subject: [PATCH 1/2] feat: update ts solution to lc/lcof2 problems --- .../README.md" | 18 +++++++++++- .../Solution.ts" | 2 +- .../README.md" | 21 ++++++++++++-- .../Solution.ts" | 6 ++-- .../README.md | 29 ++++++++++++++----- .../README_EN.md | 29 ++++++++++++++----- .../Solution.ts | 13 ++++----- 7 files changed, 89 insertions(+), 29 deletions(-) diff --git "a/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/README.md" index f8daf237bd803..34806b3c6435a 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/README.md" @@ -172,7 +172,7 @@ var lengthOfLongestSubstring = function (s) { ```ts function lengthOfLongestSubstring(s: string): number { let ans = 0; - let vis = new Set(); + const vis = new Set(); for (let i = 0, j = 0; i < s.length; ++i) { while (vis.has(s[i])) { vis.delete(s[j++]); @@ -184,6 +184,22 @@ function lengthOfLongestSubstring(s: string): number { } ``` +```ts +function lengthOfLongestSubstring(s: string): number { + let ans = 0; + const n = s.length; + const ss: boolean[] = new Array(128).fill(false); + for (let i = 0, j = 0; i < n; ++i) { + while (ss[s[i]]) { + ss[s[j++]] = false; + } + ss[s[i]] = true; + ans = Math.max(ans, i - j + 1); + } + return ans; +} +``` + ### **Rust** ```rust diff --git "a/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/Solution.ts" "b/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/Solution.ts" index 33e96ab61acb8..9ecfb9cbd55d6 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/Solution.ts" +++ "b/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/Solution.ts" @@ -1,6 +1,6 @@ function lengthOfLongestSubstring(s: string): number { let ans = 0; - let vis = new Set(); + const vis = new Set(); for (let i = 0, j = 0; i < s.length; ++i) { while (vis.has(s[i])) { vis.delete(s[j++]); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 016. \344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\345\255\227\347\254\246\344\270\262/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 016. \344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\345\255\227\347\254\246\344\270\262/README.md" index 1781e46b17c72..cd40250e6f9d2 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 016. \344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\345\255\227\347\254\246\344\270\262/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 016. \344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\345\255\227\347\254\246\344\270\262/README.md" @@ -156,16 +156,31 @@ func max(a, b int) int { ### **TypeScript** +```ts +function lengthOfLongestSubstring(s: string): number { + let ans = 0; + const vis = new Set(); + for (let i = 0, j = 0; i < s.length; ++i) { + while (vis.has(s[i])) { + vis.delete(s[j++]); + } + vis.add(s[i]); + ans = Math.max(ans, i - j + 1); + } + return ans; +} +``` + ```ts function lengthOfLongestSubstring(s: string): number { let ans = 0; const n = s.length; const ss: boolean[] = new Array(128).fill(false); for (let i = 0, j = 0; i < n; ++i) { - while (ss[s.charCodeAt(i)]) { - ss[s.charCodeAt(j++)] = false; + while (ss[s[i]]) { + ss[s[j++]] = false; } - ss[s.charCodeAt(i)] = true; + ss[s[i]] = true; ans = Math.max(ans, i - j + 1); } return ans; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 016. \344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\345\255\227\347\254\246\344\270\262/Solution.ts" "b/lcof2/\345\211\221\346\214\207 Offer II 016. \344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\345\255\227\347\254\246\344\270\262/Solution.ts" index dcca7221bd396..ec55dc4dab91a 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 016. \344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\345\255\227\347\254\246\344\270\262/Solution.ts" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 016. \344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\345\255\227\347\254\246\344\270\262/Solution.ts" @@ -3,10 +3,10 @@ function lengthOfLongestSubstring(s: string): number { const n = s.length; const ss: boolean[] = new Array(128).fill(false); for (let i = 0, j = 0; i < n; ++i) { - while (ss[s.charCodeAt(i)]) { - ss[s.charCodeAt(j++)] = false; + while (ss[s[i]]) { + ss[s[j++]] = false; } - ss[s.charCodeAt(i)] = true; + ss[s[i]] = true; ans = Math.max(ans, i - j + 1); } return ans; 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 4c0f519bc0dc4..1efd07752a3a1 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 @@ -201,15 +201,30 @@ public class Solution { ```ts function lengthOfLongestSubstring(s: string): number { - const ss = new Set(); - let i = 0; let ans = 0; - for (let j = 0; j < s.length; ++j) { - while (ss.has(s[j])) { - ss.delete(s[i++]); + const vis = new Set(); + for (let i = 0, j = 0; i < s.length; ++i) { + while (vis.has(s[i])) { + vis.delete(s[j++]); } - ss.add(s[j]); - ans = Math.max(ans, j - i + 1); + vis.add(s[i]); + ans = Math.max(ans, i - j + 1); + } + return ans; +} +``` + +```ts +function lengthOfLongestSubstring(s: string): number { + let ans = 0; + const n = s.length; + const ss: boolean[] = new Array(128).fill(false); + for (let i = 0, j = 0; i < n; ++i) { + while (ss[s[i]]) { + ss[s[j++]] = false; + } + ss[s[i]] = true; + ans = Math.max(ans, i - j + 1); } return ans; } 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 8dd59d3214139..59b6ae44a2ff2 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 @@ -191,15 +191,30 @@ public class Solution { ```ts function lengthOfLongestSubstring(s: string): number { - const ss = new Set(); - let i = 0; let ans = 0; - for (let j = 0; j < s.length; ++j) { - while (ss.has(s[j])) { - ss.delete(s[i++]); + const vis = new Set(); + for (let i = 0, j = 0; i < s.length; ++i) { + while (vis.has(s[i])) { + vis.delete(s[j++]); } - ss.add(s[j]); - ans = Math.max(ans, j - i + 1); + vis.add(s[i]); + ans = Math.max(ans, i - j + 1); + } + return ans; +} +``` + +```ts +function lengthOfLongestSubstring(s: string): number { + let ans = 0; + const n = s.length; + const ss: boolean[] = new Array(128).fill(false); + for (let i = 0, j = 0; i < n; ++i) { + while (ss[s[i]]) { + ss[s[j++]] = false; + } + ss[s[i]] = true; + ans = Math.max(ans, i - j + 1); } return ans; } 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 index ce4a8522e7a66..9ecfb9cbd55d6 100644 --- a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.ts +++ b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.ts @@ -1,13 +1,12 @@ function lengthOfLongestSubstring(s: string): number { - const ss = new Set(); - let i = 0; let ans = 0; - for (let j = 0; j < s.length; ++j) { - while (ss.has(s[j])) { - ss.delete(s[i++]); + const vis = new Set(); + for (let i = 0, j = 0; i < s.length; ++i) { + while (vis.has(s[i])) { + vis.delete(s[j++]); } - ss.add(s[j]); - ans = Math.max(ans, j - i + 1); + vis.add(s[i]); + ans = Math.max(ans, i - j + 1); } return ans; } From f23d24a04856696ffa113c89409e76124a4183f5 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 19 Aug 2023 09:18:22 +0000 Subject: [PATCH 2/2] feat: add solutions to lc/lcof problems --- .../README.md" | 63 ++++++++++++++++++- .../Solution.js" | 2 +- .../README.md | 61 ++++++++++++++++++ .../README_EN.md | 61 ++++++++++++++++++ 4 files changed, 185 insertions(+), 2 deletions(-) diff --git "a/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/README.md" index 34806b3c6435a..fb717c76114e6 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/README.md" @@ -101,6 +101,25 @@ class Solution { } ``` +```java +class Solution { + public int lengthOfLongestSubstring(String s) { + boolean[] ss = new boolean[128]; + int ans = 0, j = 0; + int n = s.length(); + for (int i = 0; i < n; ++i) { + char c = s.charAt(i); + while (ss[c]) { + ss[s.charAt(j++)] = false; + } + ans = Math.max(ans, i - j + 1); + ss[c] = true; + } + return ans; + } +} +``` + ### **C++** ```cpp @@ -121,6 +140,25 @@ public: }; ``` +```cpp +class Solution { +public: + int lengthOfLongestSubstring(string s) { + bool ss[128] = {false}; + int n = s.size(); + int ans = 0; + for (int i = 0, j = 0; i < n; ++i) { + while (ss[s[i]]) { + ss[s[j++]] = false; + } + ss[s[i]] = true; + ans = max(ans, i - j + 1); + } + return ans; + } +}; +``` + ### **Go** ```go @@ -146,6 +184,29 @@ func max(a, b int) int { } ``` +```go +func lengthOfLongestSubstring(s string) (ans int) { + ss := make([]bool, 128) + j := 0 + for i, c := range s { + for ss[c] { + ss[s[j]] = false + j++ + } + ss[c] = true + ans = max(ans, i-j+1) + } + return +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` + ### **JavaScript** ```js @@ -155,7 +216,7 @@ func max(a, b int) int { */ var lengthOfLongestSubstring = function (s) { let ans = 0; - let vis = new Set(); + const vis = new Set(); for (let i = 0, j = 0; i < s.length; ++i) { while (vis.has(s[i])) { vis.delete(s[j++]); diff --git "a/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/Solution.js" index 0297ae440820f..d0a0a610fdc5c 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/Solution.js" +++ "b/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/Solution.js" @@ -4,7 +4,7 @@ */ var lengthOfLongestSubstring = function (s) { let ans = 0; - let vis = new Set(); + const vis = new Set(); for (let i = 0, j = 0; i < s.length; ++i) { while (vis.has(s[i])) { vis.delete(s[j++]); 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 1efd07752a3a1..00f6d615bdd36 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 @@ -111,6 +111,25 @@ class Solution { } ``` +```java +class Solution { + public int lengthOfLongestSubstring(String s) { + boolean[] ss = new boolean[128]; + int ans = 0, j = 0; + int n = s.length(); + for (int i = 0; i < n; ++i) { + char c = s.charAt(i); + while (ss[c]) { + ss[s.charAt(j++)] = false; + } + ans = Math.max(ans, i - j + 1); + ss[c] = true; + } + return ans; + } +} +``` + ### **C++** ```cpp @@ -129,6 +148,25 @@ public: }; ``` +```cpp +class Solution { +public: + int lengthOfLongestSubstring(string s) { + bool ss[128] = {false}; + int n = s.size(); + int ans = 0; + for (int i = 0, j = 0; i < n; ++i) { + while (ss[s[i]]) { + ss[s[j++]] = false; + } + ss[s[i]] = true; + ans = max(ans, i - j + 1); + } + return ans; + } +}; +``` + ### **Go** ```go @@ -154,6 +192,29 @@ func max(a, b int) int { } ``` +```go +func lengthOfLongestSubstring(s string) (ans int) { + ss := make([]bool, 128) + j := 0 + for i, c := range s { + for ss[c] { + ss[s[j]] = false + j++ + } + ss[c] = true + ans = max(ans, i-j+1) + } + return +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` + ### **JavaScript** ```js 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 59b6ae44a2ff2..12370e2e1b3fe 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 @@ -101,6 +101,25 @@ class Solution { } ``` +```java +class Solution { + public int lengthOfLongestSubstring(String s) { + boolean[] ss = new boolean[128]; + int ans = 0, j = 0; + int n = s.length(); + for (int i = 0; i < n; ++i) { + char c = s.charAt(i); + while (ss[c]) { + ss[s.charAt(j++)] = false; + } + ans = Math.max(ans, i - j + 1); + ss[c] = true; + } + return ans; + } +} +``` + ### **C++** ```cpp @@ -119,6 +138,25 @@ public: }; ``` +```cpp +class Solution { +public: + int lengthOfLongestSubstring(string s) { + bool ss[128] = {false}; + int n = s.size(); + int ans = 0; + for (int i = 0, j = 0; i < n; ++i) { + while (ss[s[i]]) { + ss[s[j++]] = false; + } + ss[s[i]] = true; + ans = max(ans, i - j + 1); + } + return ans; + } +}; +``` + ### **Go** ```go @@ -144,6 +182,29 @@ func max(a, b int) int { } ``` +```go +func lengthOfLongestSubstring(s string) (ans int) { + ss := make([]bool, 128) + j := 0 + for i, c := range s { + for ss[c] { + ss[s[j]] = false + j++ + } + ss[c] = true + ans = max(ans, i-j+1) + } + return +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` + ### **JavaScript** ```js