Skip to content

Commit a6a98bb

Browse files
committed
feat: add solutions to lcof problems: No.58.1,58.2
1 parent 4288f4a commit a6a98bb

File tree

4 files changed

+43
-49
lines changed

4 files changed

+43
-49
lines changed

lcof/面试题58 - I. 翻转单词顺序/README.md

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@
4242

4343
## 解法
4444

45-
按空格分割字符串后逆序。
45+
**方法一:字符串分割 + 反转拼接**
46+
47+
我们先去除字符串首尾的空格,然后将字符串按照空格分割成数组,再将数组反转,最后将数组拼接成以空格分割的字符串即可。
48+
49+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串长度。
4650

4751
<!-- tabs:start -->
4852

@@ -51,30 +55,22 @@
5155
```python
5256
class Solution:
5357
def reverseWords(self, s: str) -> str:
54-
if s is None:
55-
return s
56-
return ' '.join(list(filter(lambda x: x != '', s.strip(' ').split(' ')))[::-1])
58+
return " ".join(s.strip().split()[::-1])
5759
```
5860

5961
### **Java**
6062

6163
```java
6264
class Solution {
6365
public String reverseWords(String s) {
64-
if (s == null || s.length() == 0) {
65-
return s;
66+
s = s.trim();
67+
var words = s.split("\\s+");
68+
for (int i = 0, j = words.length - 1; i < j; ++i, --j) {
69+
var t = words[i];
70+
words[i] = words[j];
71+
words[j] = t;
6672
}
67-
String[] words = s.split("\\s+");
68-
StringBuilder sb = new StringBuilder();
69-
int len = words.length;
70-
for (int i = len - 1; i >= 0; --i) {
71-
if (!"".equals(words[i])) {
72-
sb.append(words[i]).append(" ");
73-
}
74-
}
75-
s = sb.toString();
76-
len = s.length();
77-
return len > 0 ? s.substring(0, len - 1) : "";
73+
return String.join(" ", words);
7874
}
7975
}
8076
```
@@ -104,22 +100,6 @@ public:
104100
};
105101
```
106102
107-
### **JavaScript**
108-
109-
```js
110-
/**
111-
* @param {string} s
112-
* @return {string}
113-
*/
114-
var reverseWords = function (s) {
115-
return s
116-
.split(' ')
117-
.reduce((acc, cur) => (cur !== '' ? acc.concat(cur) : acc), [])
118-
.reverse()
119-
.join(' ');
120-
};
121-
```
122-
123103
### **Go**
124104
125105
```go
@@ -143,6 +123,22 @@ func reverseWords(s string) string {
143123
}
144124
```
145125

126+
### **JavaScript**
127+
128+
```js
129+
/**
130+
* @param {string} s
131+
* @return {string}
132+
*/
133+
var reverseWords = function (s) {
134+
return s
135+
.split(' ')
136+
.reduce((acc, cur) => (cur !== '' ? acc.concat(cur) : acc), [])
137+
.reverse()
138+
.join(' ');
139+
};
140+
```
141+
146142
### **TypeScript**
147143

148144
API:
Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
class Solution {
22
public String reverseWords(String s) {
3-
if (s == null || s.length() == 0) {
4-
return s;
3+
s = s.trim();
4+
var words = s.split("\\s+");
5+
for (int i = 0, j = words.length - 1; i < j; ++i, --j) {
6+
var t = words[i];
7+
words[i] = words[j];
8+
words[j] = t;
59
}
6-
String[] words = s.split("\\s+");
7-
StringBuilder sb = new StringBuilder();
8-
int len = words.length;
9-
for (int i = len - 1; i >= 0; --i) {
10-
if (!"".equals(words[i])) {
11-
sb.append(words[i]).append(" ");
12-
}
13-
}
14-
s = sb.toString();
15-
len = s.length();
16-
return len > 0 ? s.substring(0, len - 1) : "";
10+
return String.join(" ", words);
1711
}
1812
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
class Solution:
22
def reverseWords(self, s: str) -> str:
3-
if s is None:
4-
return s
5-
return ' '.join(list(filter(lambda x: x != '', s.strip(' ').split(' ')))[::-1])
3+
return " ".join(s.strip().split()[::-1])

lcof/面试题58 - II. 左旋转字符串/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828

2929
## 解法
3030

31+
**方法一:模拟**
32+
33+
我们可以将字符串分为两部分,分别对两部分进行翻转,然后再对整个字符串进行翻转,即可得到结果。或者直接截取两个子串,然后拼接起来。
34+
35+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串长度。
36+
3137
<!-- tabs:start -->
3238

3339
### **Python3**

0 commit comments

Comments
 (0)