Skip to content

Commit e722f1d

Browse files
committed
feat: add solutions to lcof problem: No.58 - |
面试题58 - I. 翻转单词顺序
1 parent c6aa860 commit e722f1d

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

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

+48
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,54 @@ func reverseWords(s string) string {
115115
}
116116
```
117117

118+
### **TypeScript**
119+
120+
API:
121+
122+
```ts
123+
function reverseWords(s: string): string {
124+
return s.trim().split(/\s+/).reverse().join(' ');
125+
}
126+
```
127+
128+
双指针:
129+
130+
```ts
131+
function reverseWords(s: string): string {
132+
s = s.trim();
133+
const res = [];
134+
let l = s.length - 1;
135+
let r = s.length - 1;
136+
while (l >= 0) {
137+
while (s[l] !== ' ' && l >= 0) {
138+
l--;
139+
}
140+
res.push(s.substring(l + 1, r + 1));
141+
while (s[l] === ' ' && l >= 0) {
142+
l--;
143+
}
144+
r = l;
145+
}
146+
return res.join(' ');
147+
}
148+
```
149+
150+
### **Rust**
151+
152+
```rust
153+
impl Solution {
154+
pub fn reverse_words(mut s: String) -> String {
155+
let mut res = s.trim().split(' ').rev().collect::<Vec<&str>>();
156+
for i in (0..res.len()).rev() {
157+
if res[i] == "" {
158+
res.remove(i);
159+
}
160+
}
161+
res.join(" ")
162+
}
163+
}
164+
```
165+
118166
### **...**
119167

120168
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
impl Solution {
2+
pub fn reverse_words(mut s: String) -> String {
3+
let mut res = s.trim().split(' ').rev().collect::<Vec<&str>>();
4+
for i in (0..res.len()).rev() {
5+
if res[i] == "" {
6+
res.remove(i);
7+
}
8+
}
9+
res.join(" ")
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function reverseWords(s: string): string {
2+
return s.trim().split(/\s+/).reverse().join(' ');
3+
}

0 commit comments

Comments
 (0)