Skip to content

Commit 473f026

Browse files
committed
feat: update lcci solutions: No.01.03. String to URL
1 parent 5d76143 commit 473f026

File tree

6 files changed

+90
-34
lines changed

6 files changed

+90
-34
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
- [选择排序](/basic/sorting/SelectionSort/README.md)
4040
- [归并排序](/basic/sorting/MergeSort/README.md)
4141
- [快速排序](/basic/sorting/QuickSort/README.md)
42+
- [希尔排序](/basic/sorting/ShellSort/README.md)
4243

4344
### 查找算法
4445

lcci/01.03.String to URL/README.md

+35-12
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,23 @@
3333

3434
### **Python3**
3535

36-
<!-- 这里可写当前语言的特殊实现逻辑 -->
36+
直接利用 `replace` 将所有 ` ` 替换为 `%20`
3737

3838
```python
3939
class Solution:
4040
def replaceSpaces(self, S: str, length: int) -> str:
41-
S = S[:length] if length < len(S) else S
42-
return S.replace(' ', '%20')
41+
return S[:length].replace(' ', '%20')
42+
```
43+
44+
初始化一个空列表 `chars`,遍历字符串中每个字符 `c`,遇到空格字符时,往 `chars` 中追加元素 `%20`,否则追加 `c`。最后返回 `''.join(chars)` 即可。
45+
46+
```python
47+
class Solution:
48+
def replaceSpaces(self, S: str, length: int) -> str:
49+
chars = []
50+
for c in S[:length]:
51+
chars.append('%20' if c == ' ' else c)
52+
return ''.join(chars)
4353
```
4454

4555
### **Java**
@@ -49,22 +59,35 @@ class Solution:
4959
```java
5060
class Solution {
5161
public String replaceSpaces(String S, int length) {
52-
char[] c = S.toCharArray();
53-
int j = c.length;
54-
for (int i = length - 1; i >= 0; i--) {
55-
if (c[i] == ' ') {
56-
c[--j] = '0';
57-
c[--j] = '2';
58-
c[--j] = '%';
62+
char[] chars = S.toCharArray();
63+
int j = chars.length;
64+
for (int i = length - 1; i >= 0; --i) {
65+
if (chars[i] == ' ') {
66+
chars[--j] = '0';
67+
chars[--j] = '2';
68+
chars[--j] = '%';
5969
} else {
60-
c[--j] = c[i];
70+
chars[--j] = chars[i];
6171
}
6272
}
63-
return new String(c, j, c.length - j);
73+
return new String(chars, j, chars.length - j);
6474
}
6575
}
6676
```
6777

78+
### JavaScript
79+
80+
```js
81+
/**
82+
* @param {string} S
83+
* @param {number} length
84+
* @return {string}
85+
*/
86+
var replaceSpaces = function(S, length) {
87+
return encodeURI(S.substring(0,length));
88+
};
89+
```
90+
6891
### **...**
6992

7093
```

lcci/01.03.String to URL/README_EN.md

+36-11
Original file line numberDiff line numberDiff line change
@@ -44,34 +44,59 @@ The missing numbers are [5,6,8,...], hence the third missing number is 8.
4444

4545
### **Python3**
4646

47+
Use `replace()` function.
48+
4749
```python
4850
class Solution:
4951
def replaceSpaces(self, S: str, length: int) -> str:
50-
S = S[:length] if length < len(S) else S
51-
return S.replace(' ', '%20')
52+
return S[:length].replace(' ', '%20')
53+
```
54+
55+
Use list:
56+
57+
```python
58+
class Solution:
59+
def replaceSpaces(self, S: str, length: int) -> str:
60+
chars = []
61+
for c in S[:length]:
62+
chars.append('%20' if c == ' ' else c)
63+
return ''.join(chars)
5264
```
5365

5466
### **Java**
5567

5668
```java
5769
class Solution {
5870
public String replaceSpaces(String S, int length) {
59-
char[] c = S.toCharArray();
60-
int j = c.length;
61-
for (int i = length - 1; i >= 0; i--) {
62-
if (c[i] == ' ') {
63-
c[--j] = '0';
64-
c[--j] = '2';
65-
c[--j] = '%';
71+
char[] chars = S.toCharArray();
72+
int j = chars.length;
73+
for (int i = length - 1; i >= 0; --i) {
74+
if (chars[i] == ' ') {
75+
chars[--j] = '0';
76+
chars[--j] = '2';
77+
chars[--j] = '%';
6678
} else {
67-
c[--j] = c[i];
79+
chars[--j] = chars[i];
6880
}
6981
}
70-
return new String(c, j, c.length - j);
82+
return new String(chars, j, chars.length - j);
7183
}
7284
}
7385
```
7486

87+
### JavaScript
88+
89+
```js
90+
/**
91+
* @param {string} S
92+
* @param {number} length
93+
* @return {string}
94+
*/
95+
var replaceSpaces = function(S, length) {
96+
return encodeURI(S.substring(0,length));
97+
};
98+
```
99+
75100
### **...**
76101

77102
```
+9-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
class Solution {
22
public String replaceSpaces(String S, int length) {
3-
char[] c = S.toCharArray();
4-
int j = c.length;
5-
for (int i = length - 1; i >= 0; i--) {
6-
if (c[i] == ' ') {
7-
c[--j] = '0';
8-
c[--j] = '2';
9-
c[--j] = '%';
3+
char[] chars = S.toCharArray();
4+
int j = chars.length;
5+
for (int i = length - 1; i >= 0; --i) {
6+
if (chars[i] == ' ') {
7+
chars[--j] = '0';
8+
chars[--j] = '2';
9+
chars[--j] = '%';
1010
} else {
11-
c[--j] = c[i];
11+
chars[--j] = chars[i];
1212
}
1313
}
14-
return new String(c, j, c.length - j);
14+
return new String(chars, j, chars.length - j);
1515
}
1616
}

lcci/01.03.String to URL/Solution.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @param {string} S
3+
* @param {number} length
4+
* @return {string}
5+
*/
6+
var replaceSpaces = function(S, length) {
7+
return encodeURI(S.substring(0,length));
8+
};

lcci/01.03.String to URL/Solution.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
class Solution:
22
def replaceSpaces(self, S: str, length: int) -> str:
3-
S = S[:length] if length < len(S) else S
4-
return S.replace(' ', '%20')
3+
return S[:length].replace(' ', '%20')

0 commit comments

Comments
 (0)