diff --git a/lcci/01.03.String to URL/README.md b/lcci/01.03.String to URL/README.md index 3dc83a075beec..c09371e55de65 100644 --- a/lcci/01.03.String to URL/README.md +++ b/lcci/01.03.String to URL/README.md @@ -41,45 +41,6 @@ class Solution: return S[:length].replace(' ', '%20') ``` -```java -class Solution { - public String replaceSpaces(String S, int length) { - char[] cs = S.toCharArray(); - int j = cs.length; - for (int i = length - 1; i >= 0; --i) { - if (cs[i] == ' ') { - cs[--j] = '0'; - cs[--j] = '2'; - cs[--j] = '%'; - } else { - cs[--j] = cs[i]; - } - } - return new String(cs, j, cs.length - j); - } -} -``` - -```go -func replaceSpaces(S string, length int) string { - // return url.PathEscape(S[:length]) - j := len(S) - b := []byte(S) - for i := length - 1; i >= 0; i-- { - if b[i] == ' ' { - b[j-1] = '0' - b[j-2] = '2' - b[j-3] = '%' - j -= 3 - } else { - b[j-1] = b[i] - j-- - } - } - return string(b[j:]) -} -``` - ```ts function replaceSpaces(S: string, length: number): string { return S.slice(0, length).replace(/\s/g, '%20'); @@ -121,6 +82,45 @@ class Solution: return ''.join(['%20' if c == ' ' else c for c in S[:length]]) ``` +```java +class Solution { + public String replaceSpaces(String S, int length) { + char[] cs = S.toCharArray(); + int j = cs.length; + for (int i = length - 1; i >= 0; --i) { + if (cs[i] == ' ') { + cs[--j] = '0'; + cs[--j] = '2'; + cs[--j] = '%'; + } else { + cs[--j] = cs[i]; + } + } + return new String(cs, j, cs.length - j); + } +} +``` + +```go +func replaceSpaces(S string, length int) string { + // return url.PathEscape(S[:length]) + j := len(S) + b := []byte(S) + for i := length - 1; i >= 0; i-- { + if b[i] == ' ' { + b[j-1] = '0' + b[j-2] = '2' + b[j-3] = '%' + j -= 3 + } else { + b[j-1] = b[i] + j-- + } + } + return string(b[j:]) +} +``` + ```rust impl Solution { pub fn replace_spaces(s: String, length: i32) -> String { diff --git a/lcci/01.03.String to URL/README_EN.md b/lcci/01.03.String to URL/README_EN.md index 2238c831f4b5d..84d681d9925e7 100644 --- a/lcci/01.03.String to URL/README_EN.md +++ b/lcci/01.03.String to URL/README_EN.md @@ -54,45 +54,6 @@ class Solution: return S[:length].replace(' ', '%20') ``` -```java -class Solution { - public String replaceSpaces(String S, int length) { - char[] cs = S.toCharArray(); - int j = cs.length; - for (int i = length - 1; i >= 0; --i) { - if (cs[i] == ' ') { - cs[--j] = '0'; - cs[--j] = '2'; - cs[--j] = '%'; - } else { - cs[--j] = cs[i]; - } - } - return new String(cs, j, cs.length - j); - } -} -``` - -```go -func replaceSpaces(S string, length int) string { - // return url.PathEscape(S[:length]) - j := len(S) - b := []byte(S) - for i := length - 1; i >= 0; i-- { - if b[i] == ' ' { - b[j-1] = '0' - b[j-2] = '2' - b[j-3] = '%' - j -= 3 - } else { - b[j-1] = b[i] - j-- - } - } - return string(b[j:]) -} -``` - ```ts function replaceSpaces(S: string, length: number): string { return S.slice(0, length).replace(/\s/g, '%20'); @@ -134,6 +95,45 @@ class Solution: return ''.join(['%20' if c == ' ' else c for c in S[:length]]) ``` +```java +class Solution { + public String replaceSpaces(String S, int length) { + char[] cs = S.toCharArray(); + int j = cs.length; + for (int i = length - 1; i >= 0; --i) { + if (cs[i] == ' ') { + cs[--j] = '0'; + cs[--j] = '2'; + cs[--j] = '%'; + } else { + cs[--j] = cs[i]; + } + } + return new String(cs, j, cs.length - j); + } +} +``` + +```go +func replaceSpaces(S string, length int) string { + // return url.PathEscape(S[:length]) + j := len(S) + b := []byte(S) + for i := length - 1; i >= 0; i-- { + if b[i] == ' ' { + b[j-1] = '0' + b[j-2] = '2' + b[j-3] = '%' + j -= 3 + } else { + b[j-1] = b[i] + j-- + } + } + return string(b[j:]) +} +``` + ```rust impl Solution { pub fn replace_spaces(s: String, length: i32) -> String { diff --git a/lcci/01.03.String to URL/Solution.go b/lcci/01.03.String to URL/Solution2.go similarity index 100% rename from lcci/01.03.String to URL/Solution.go rename to lcci/01.03.String to URL/Solution2.go diff --git a/lcci/01.03.String to URL/Solution.java b/lcci/01.03.String to URL/Solution2.java similarity index 100% rename from lcci/01.03.String to URL/Solution.java rename to lcci/01.03.String to URL/Solution2.java diff --git a/lcci/01.03.String to URL/Solution2.py b/lcci/01.03.String to URL/Solution2.py index e64fda706fc0c..2f05694a706c5 100644 --- a/lcci/01.03.String to URL/Solution2.py +++ b/lcci/01.03.String to URL/Solution2.py @@ -1,3 +1,3 @@ class Solution: def replaceSpaces(self, S: str, length: int) -> str: - return ''.join(['%20' if c == ' ' else c for c in S[:length]]) + return "".join(["%20" if c == " " else c for c in S[:length]]) diff --git a/lcci/01.04.Palindrome Permutation/README.md b/lcci/01.04.Palindrome Permutation/README.md index 34c7a377036b9..8f209949ca354 100644 --- a/lcci/01.04.Palindrome Permutation/README.md +++ b/lcci/01.04.Palindrome Permutation/README.md @@ -122,7 +122,13 @@ impl Solution { -### 方法二 +### 方法二:哈希表的另一种实现 + +我们用哈希表 $vis$ 存储每个字符是否出现过。若出现过,则从哈希表中删除该字符;否则,将该字符加入哈希表。 + +最后判断哈希表中字符的个数是否小于 $2$,若是,则是回文排列。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串长度。 diff --git a/lcci/01.04.Palindrome Permutation/README_EN.md b/lcci/01.04.Palindrome Permutation/README_EN.md index e557f7a324db0..155a8c2885f98 100644 --- a/lcci/01.04.Palindrome Permutation/README_EN.md +++ b/lcci/01.04.Palindrome Permutation/README_EN.md @@ -119,7 +119,13 @@ impl Solution { -### Solution 2 +### Solution 2: Another Implementation of Hash Table + +We use a hash table $vis$ to store whether each character has appeared. If it has appeared, we remove the character from the hash table; otherwise, we add the character to the hash table. + +Finally, we check whether the number of characters in the hash table is less than $2$. If it is, then it is a palindrome permutation. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string. diff --git a/lcci/01.06.Compress String/README.md b/lcci/01.06.Compress String/README.md index 7018975f69f2f..c6bed3d949bac 100644 --- a/lcci/01.06.Compress String/README.md +++ b/lcci/01.06.Compress String/README.md @@ -47,6 +47,20 @@ class Solution: return min(S, t, key=len) ``` +```python +class Solution: + def compressString(self, S: str) -> str: + t = [] + i, n = 0, len(S) + while i < n: + j = i + 1 + while j < n and S[j] == S[i]: + j += 1 + t.append(S[i] + str(j - i)) + i = j + return min(S, "".join(t), key=len) +``` + ```java class Solution { public String compressString(String S) { @@ -155,24 +169,4 @@ var compressString = function (S) { -### 方法二 - - - -```python -class Solution: - def compressString(self, S: str) -> str: - t = [] - i, n = 0, len(S) - while i < n: - j = i + 1 - while j < n and S[j] == S[i]: - j += 1 - t.append(S[i] + str(j - i)) - i = j - return min(S, "".join(t), key=len) -``` - - - diff --git a/lcci/01.06.Compress String/README_EN.md b/lcci/01.06.Compress String/README_EN.md index bc76535775a71..4b2863013fc94 100644 --- a/lcci/01.06.Compress String/README_EN.md +++ b/lcci/01.06.Compress String/README_EN.md @@ -53,6 +53,20 @@ class Solution: return min(S, t, key=len) ``` +```python +class Solution: + def compressString(self, S: str) -> str: + t = [] + i, n = 0, len(S) + while i < n: + j = i + 1 + while j < n and S[j] == S[i]: + j += 1 + t.append(S[i] + str(j - i)) + i = j + return min(S, "".join(t), key=len) +``` + ```java class Solution { public String compressString(String S) { @@ -161,24 +175,4 @@ var compressString = function (S) { -### Solution 2 - - - -```python -class Solution: - def compressString(self, S: str) -> str: - t = [] - i, n = 0, len(S) - while i < n: - j = i + 1 - while j < n and S[j] == S[i]: - j += 1 - t.append(S[i] + str(j - i)) - i = j - return min(S, "".join(t), key=len) -``` - - - diff --git a/lcci/01.09.String Rotation/README.md b/lcci/01.09.String Rotation/README.md index 35a96810b7d0d..f4cd5d44209ef 100644 --- a/lcci/01.09.String Rotation/README.md +++ b/lcci/01.09.String Rotation/README.md @@ -104,39 +104,4 @@ impl Solution { -### 方法二 - - - -```rust -impl Solution { - pub fn is_fliped_string(s1: String, s2: String) -> bool { - if s1 == s2 { - return true; - } - if s1.len() != s2.len() { - return false; - } - let s2: Vec = (s2.clone() + &s2).chars().collect(); - let n = s1.len(); - let m = s2.len(); - for i in 0..m - n { - let mut is_pass = true; - for (j, c) in s1.chars().enumerate() { - if c != s2[i + j] { - is_pass = false; - break; - } - } - if is_pass { - return true; - } - } - false - } -} -``` - - - diff --git a/lcci/01.09.String Rotation/README_EN.md b/lcci/01.09.String Rotation/README_EN.md index 7cb3f64964478..700ac0284fca4 100644 --- a/lcci/01.09.String Rotation/README_EN.md +++ b/lcci/01.09.String Rotation/README_EN.md @@ -104,39 +104,4 @@ impl Solution { -### Solution 2 - - - -```rust -impl Solution { - pub fn is_fliped_string(s1: String, s2: String) -> bool { - if s1 == s2 { - return true; - } - if s1.len() != s2.len() { - return false; - } - let s2: Vec = (s2.clone() + &s2).chars().collect(); - let n = s1.len(); - let m = s2.len(); - for i in 0..m - n { - let mut is_pass = true; - for (j, c) in s1.chars().enumerate() { - if c != s2[i + j] { - is_pass = false; - break; - } - } - if is_pass { - return true; - } - } - false - } -} -``` - - - diff --git a/lcci/01.09.String Rotation/Solution2.rs b/lcci/01.09.String Rotation/Solution2.rs deleted file mode 100644 index 56f1489dcc677..0000000000000 --- a/lcci/01.09.String Rotation/Solution2.rs +++ /dev/null @@ -1,26 +0,0 @@ -impl Solution { - pub fn is_fliped_string(s1: String, s2: String) -> bool { - if s1 == s2 { - return true; - } - if s1.len() != s2.len() { - return false; - } - let s2: Vec = (s2.clone() + &s2).chars().collect(); - let n = s1.len(); - let m = s2.len(); - for i in 0..m - n { - let mut is_pass = true; - for (j, c) in s1.chars().enumerate() { - if c != s2[i + j] { - is_pass = false; - break; - } - } - if is_pass { - return true; - } - } - false - } -} diff --git a/lcci/02.01.Remove Duplicate Node/README.md b/lcci/02.01.Remove Duplicate Node/README.md index db19f23ba90bf..e36408c701fd9 100644 --- a/lcci/02.01.Remove Duplicate Node/README.md +++ b/lcci/02.01.Remove Duplicate Node/README.md @@ -248,40 +248,4 @@ var removeDuplicateNodes = function (head) { -### 方法二 - - - -```ts -/** - * Definition for singly-linked list. - * class ListNode { - * val: number - * next: ListNode | null - * constructor(val?: number, next?: ListNode | null) { - * this.val = (val===undefined ? 0 : val) - * this.next = (next===undefined ? null : next) - * } - * } - */ - -function removeDuplicateNodes(head: ListNode | null): ListNode | null { - let n1 = head; - while (n1 != null) { - let n2 = n1; - while (n2.next != null) { - if (n1.val === n2.next.val) { - n2.next = n2.next.next; - } else { - n2 = n2.next; - } - } - n1 = n1.next; - } - return head; -} -``` - - - diff --git a/lcci/02.01.Remove Duplicate Node/README_EN.md b/lcci/02.01.Remove Duplicate Node/README_EN.md index 4b43fc5a15003..3a03dbf90b9ed 100644 --- a/lcci/02.01.Remove Duplicate Node/README_EN.md +++ b/lcci/02.01.Remove Duplicate Node/README_EN.md @@ -253,40 +253,4 @@ var removeDuplicateNodes = function (head) { -### Solution 2 - - - -```ts -/** - * Definition for singly-linked list. - * class ListNode { - * val: number - * next: ListNode | null - * constructor(val?: number, next?: ListNode | null) { - * this.val = (val===undefined ? 0 : val) - * this.next = (next===undefined ? null : next) - * } - * } - */ - -function removeDuplicateNodes(head: ListNode | null): ListNode | null { - let n1 = head; - while (n1 != null) { - let n2 = n1; - while (n2.next != null) { - if (n1.val === n2.next.val) { - n2.next = n2.next.next; - } else { - n2 = n2.next; - } - } - n1 = n1.next; - } - return head; -} -``` - - - diff --git a/lcci/02.01.Remove Duplicate Node/Solution2.ts b/lcci/02.01.Remove Duplicate Node/Solution2.ts deleted file mode 100644 index 6074af6cc05cf..0000000000000 --- a/lcci/02.01.Remove Duplicate Node/Solution2.ts +++ /dev/null @@ -1,27 +0,0 @@ -/** - * Definition for singly-linked list. - * class ListNode { - * val: number - * next: ListNode | null - * constructor(val?: number, next?: ListNode | null) { - * this.val = (val===undefined ? 0 : val) - * this.next = (next===undefined ? null : next) - * } - * } - */ - -function removeDuplicateNodes(head: ListNode | null): ListNode | null { - let n1 = head; - while (n1 != null) { - let n2 = n1; - while (n2.next != null) { - if (n1.val === n2.next.val) { - n2.next = n2.next.next; - } else { - n2 = n2.next; - } - } - n1 = n1.next; - } - return head; -}