Skip to content

Commit 5d8baa3

Browse files
authored
feat: update solutions to lcci problems (doocs#2227)
1 parent fa73a73 commit 5d8baa3

File tree

15 files changed

+121
-316
lines changed

15 files changed

+121
-316
lines changed

lcci/01.03.String to URL/README.md

+39-39
Original file line numberDiff line numberDiff line change
@@ -41,45 +41,6 @@ class Solution:
4141
return S[:length].replace(' ', '%20')
4242
```
4343

44-
```java
45-
class Solution {
46-
public String replaceSpaces(String S, int length) {
47-
char[] cs = S.toCharArray();
48-
int j = cs.length;
49-
for (int i = length - 1; i >= 0; --i) {
50-
if (cs[i] == ' ') {
51-
cs[--j] = '0';
52-
cs[--j] = '2';
53-
cs[--j] = '%';
54-
} else {
55-
cs[--j] = cs[i];
56-
}
57-
}
58-
return new String(cs, j, cs.length - j);
59-
}
60-
}
61-
```
62-
63-
```go
64-
func replaceSpaces(S string, length int) string {
65-
// return url.PathEscape(S[:length])
66-
j := len(S)
67-
b := []byte(S)
68-
for i := length - 1; i >= 0; i-- {
69-
if b[i] == ' ' {
70-
b[j-1] = '0'
71-
b[j-2] = '2'
72-
b[j-3] = '%'
73-
j -= 3
74-
} else {
75-
b[j-1] = b[i]
76-
j--
77-
}
78-
}
79-
return string(b[j:])
80-
}
81-
```
82-
8344
```ts
8445
function replaceSpaces(S: string, length: number): string {
8546
return S.slice(0, length).replace(/\s/g, '%20');
@@ -121,6 +82,45 @@ class Solution:
12182
return ''.join(['%20' if c == ' ' else c for c in S[:length]])
12283
```
12384

85+
```java
86+
class Solution {
87+
public String replaceSpaces(String S, int length) {
88+
char[] cs = S.toCharArray();
89+
int j = cs.length;
90+
for (int i = length - 1; i >= 0; --i) {
91+
if (cs[i] == ' ') {
92+
cs[--j] = '0';
93+
cs[--j] = '2';
94+
cs[--j] = '%';
95+
} else {
96+
cs[--j] = cs[i];
97+
}
98+
}
99+
return new String(cs, j, cs.length - j);
100+
}
101+
}
102+
```
103+
104+
```go
105+
func replaceSpaces(S string, length int) string {
106+
// return url.PathEscape(S[:length])
107+
j := len(S)
108+
b := []byte(S)
109+
for i := length - 1; i >= 0; i-- {
110+
if b[i] == ' ' {
111+
b[j-1] = '0'
112+
b[j-2] = '2'
113+
b[j-3] = '%'
114+
j -= 3
115+
} else {
116+
b[j-1] = b[i]
117+
j--
118+
}
119+
}
120+
return string(b[j:])
121+
}
122+
```
123+
124124
```rust
125125
impl Solution {
126126
pub fn replace_spaces(s: String, length: i32) -> String {

lcci/01.03.String to URL/README_EN.md

+39-39
Original file line numberDiff line numberDiff line change
@@ -54,45 +54,6 @@ class Solution:
5454
return S[:length].replace(' ', '%20')
5555
```
5656

57-
```java
58-
class Solution {
59-
public String replaceSpaces(String S, int length) {
60-
char[] cs = S.toCharArray();
61-
int j = cs.length;
62-
for (int i = length - 1; i >= 0; --i) {
63-
if (cs[i] == ' ') {
64-
cs[--j] = '0';
65-
cs[--j] = '2';
66-
cs[--j] = '%';
67-
} else {
68-
cs[--j] = cs[i];
69-
}
70-
}
71-
return new String(cs, j, cs.length - j);
72-
}
73-
}
74-
```
75-
76-
```go
77-
func replaceSpaces(S string, length int) string {
78-
// return url.PathEscape(S[:length])
79-
j := len(S)
80-
b := []byte(S)
81-
for i := length - 1; i >= 0; i-- {
82-
if b[i] == ' ' {
83-
b[j-1] = '0'
84-
b[j-2] = '2'
85-
b[j-3] = '%'
86-
j -= 3
87-
} else {
88-
b[j-1] = b[i]
89-
j--
90-
}
91-
}
92-
return string(b[j:])
93-
}
94-
```
95-
9657
```ts
9758
function replaceSpaces(S: string, length: number): string {
9859
return S.slice(0, length).replace(/\s/g, '%20');
@@ -134,6 +95,45 @@ class Solution:
13495
return ''.join(['%20' if c == ' ' else c for c in S[:length]])
13596
```
13697

98+
```java
99+
class Solution {
100+
public String replaceSpaces(String S, int length) {
101+
char[] cs = S.toCharArray();
102+
int j = cs.length;
103+
for (int i = length - 1; i >= 0; --i) {
104+
if (cs[i] == ' ') {
105+
cs[--j] = '0';
106+
cs[--j] = '2';
107+
cs[--j] = '%';
108+
} else {
109+
cs[--j] = cs[i];
110+
}
111+
}
112+
return new String(cs, j, cs.length - j);
113+
}
114+
}
115+
```
116+
117+
```go
118+
func replaceSpaces(S string, length int) string {
119+
// return url.PathEscape(S[:length])
120+
j := len(S)
121+
b := []byte(S)
122+
for i := length - 1; i >= 0; i-- {
123+
if b[i] == ' ' {
124+
b[j-1] = '0'
125+
b[j-2] = '2'
126+
b[j-3] = '%'
127+
j -= 3
128+
} else {
129+
b[j-1] = b[i]
130+
j--
131+
}
132+
}
133+
return string(b[j:])
134+
}
135+
```
136+
137137
```rust
138138
impl Solution {
139139
pub fn replace_spaces(s: String, length: i32) -> String {
File renamed without changes.
File renamed without changes.

lcci/01.03.String to URL/Solution2.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
class Solution:
22
def replaceSpaces(self, S: str, length: int) -> str:
3-
return ''.join(['%20' if c == ' ' else c for c in S[:length]])
3+
return "".join(["%20" if c == " " else c for c in S[:length]])

lcci/01.04.Palindrome Permutation/README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,13 @@ impl Solution {
122122

123123
<!-- tabs:end -->
124124

125-
### 方法二
125+
### 方法二:哈希表的另一种实现
126+
127+
我们用哈希表 $vis$ 存储每个字符是否出现过。若出现过,则从哈希表中删除该字符;否则,将该字符加入哈希表。
128+
129+
最后判断哈希表中字符的个数是否小于 $2$,若是,则是回文排列。
130+
131+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串长度。
126132

127133
<!-- tabs:start -->
128134

lcci/01.04.Palindrome Permutation/README_EN.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,13 @@ impl Solution {
119119

120120
<!-- tabs:end -->
121121

122-
### Solution 2
122+
### Solution 2: Another Implementation of Hash Table
123+
124+
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.
125+
126+
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.
127+
128+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string.
123129

124130
<!-- tabs:start -->
125131

lcci/01.06.Compress String/README.md

+14-20
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ class Solution:
4747
return min(S, t, key=len)
4848
```
4949

50+
```python
51+
class Solution:
52+
def compressString(self, S: str) -> str:
53+
t = []
54+
i, n = 0, len(S)
55+
while i < n:
56+
j = i + 1
57+
while j < n and S[j] == S[i]:
58+
j += 1
59+
t.append(S[i] + str(j - i))
60+
i = j
61+
return min(S, "".join(t), key=len)
62+
```
63+
5064
```java
5165
class Solution {
5266
public String compressString(String S) {
@@ -155,24 +169,4 @@ var compressString = function (S) {
155169

156170
<!-- tabs:end -->
157171

158-
### 方法二
159-
160-
<!-- tabs:start -->
161-
162-
```python
163-
class Solution:
164-
def compressString(self, S: str) -> str:
165-
t = []
166-
i, n = 0, len(S)
167-
while i < n:
168-
j = i + 1
169-
while j < n and S[j] == S[i]:
170-
j += 1
171-
t.append(S[i] + str(j - i))
172-
i = j
173-
return min(S, "".join(t), key=len)
174-
```
175-
176-
<!-- tabs:end -->
177-
178172
<!-- end -->

lcci/01.06.Compress String/README_EN.md

+14-20
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ class Solution:
5353
return min(S, t, key=len)
5454
```
5555

56+
```python
57+
class Solution:
58+
def compressString(self, S: str) -> str:
59+
t = []
60+
i, n = 0, len(S)
61+
while i < n:
62+
j = i + 1
63+
while j < n and S[j] == S[i]:
64+
j += 1
65+
t.append(S[i] + str(j - i))
66+
i = j
67+
return min(S, "".join(t), key=len)
68+
```
69+
5670
```java
5771
class Solution {
5872
public String compressString(String S) {
@@ -161,24 +175,4 @@ var compressString = function (S) {
161175

162176
<!-- tabs:end -->
163177

164-
### Solution 2
165-
166-
<!-- tabs:start -->
167-
168-
```python
169-
class Solution:
170-
def compressString(self, S: str) -> str:
171-
t = []
172-
i, n = 0, len(S)
173-
while i < n:
174-
j = i + 1
175-
while j < n and S[j] == S[i]:
176-
j += 1
177-
t.append(S[i] + str(j - i))
178-
i = j
179-
return min(S, "".join(t), key=len)
180-
```
181-
182-
<!-- tabs:end -->
183-
184178
<!-- end -->

lcci/01.09.String Rotation/README.md

-35
Original file line numberDiff line numberDiff line change
@@ -104,39 +104,4 @@ impl Solution {
104104

105105
<!-- tabs:end -->
106106

107-
### 方法二
108-
109-
<!-- tabs:start -->
110-
111-
```rust
112-
impl Solution {
113-
pub fn is_fliped_string(s1: String, s2: String) -> bool {
114-
if s1 == s2 {
115-
return true;
116-
}
117-
if s1.len() != s2.len() {
118-
return false;
119-
}
120-
let s2: Vec<char> = (s2.clone() + &s2).chars().collect();
121-
let n = s1.len();
122-
let m = s2.len();
123-
for i in 0..m - n {
124-
let mut is_pass = true;
125-
for (j, c) in s1.chars().enumerate() {
126-
if c != s2[i + j] {
127-
is_pass = false;
128-
break;
129-
}
130-
}
131-
if is_pass {
132-
return true;
133-
}
134-
}
135-
false
136-
}
137-
}
138-
```
139-
140-
<!-- tabs:end -->
141-
142107
<!-- end -->

lcci/01.09.String Rotation/README_EN.md

-35
Original file line numberDiff line numberDiff line change
@@ -104,39 +104,4 @@ impl Solution {
104104

105105
<!-- tabs:end -->
106106

107-
### Solution 2
108-
109-
<!-- tabs:start -->
110-
111-
```rust
112-
impl Solution {
113-
pub fn is_fliped_string(s1: String, s2: String) -> bool {
114-
if s1 == s2 {
115-
return true;
116-
}
117-
if s1.len() != s2.len() {
118-
return false;
119-
}
120-
let s2: Vec<char> = (s2.clone() + &s2).chars().collect();
121-
let n = s1.len();
122-
let m = s2.len();
123-
for i in 0..m - n {
124-
let mut is_pass = true;
125-
for (j, c) in s1.chars().enumerate() {
126-
if c != s2[i + j] {
127-
is_pass = false;
128-
break;
129-
}
130-
}
131-
if is_pass {
132-
return true;
133-
}
134-
}
135-
false
136-
}
137-
}
138-
```
139-
140-
<!-- tabs:end -->
141-
142107
<!-- end -->

0 commit comments

Comments
 (0)