Skip to content

Commit 076ecc9

Browse files
committed
feat: add solutions to lc problem: No.0151. Reverse Words in a String
1 parent d206d5e commit 076ecc9

File tree

5 files changed

+48
-54
lines changed

5 files changed

+48
-54
lines changed

solution/0100-0199/0151.Reverse Words in a String/README.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,34 @@
8383
<!-- 这里可写当前语言的特殊实现逻辑 -->
8484

8585
```python
86-
86+
class Solution:
87+
def reverseWords(self, s: str) -> str:
88+
words = s.strip().split()
89+
return ' '.join(words[::-1])
8790
```
8891

8992
### **Java**
9093

9194
<!-- 这里可写当前语言的特殊实现逻辑 -->
9295

9396
```java
97+
class Solution {
98+
public String reverseWords(String s) {
99+
List<String> words = Arrays.asList(s.trim().split("\\s+"));
100+
Collections.reverse(words);
101+
return String.join(" ", words);
102+
}
103+
}
104+
```
105+
106+
### **C#**
94107

108+
```cs
109+
public class Solution {
110+
public string ReverseWords(string s) {
111+
return string.Join(" ", s.Trim().Split(" ").Where(word => !string.IsNullOrEmpty(word) && !string.IsNullOrEmpty(word.Trim())).Reverse());
112+
}
113+
}
95114
```
96115

97116
### **...**

solution/0100-0199/0151.Reverse Words in a String/README_EN.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,32 @@
7070
### **Python3**
7171

7272
```python
73-
73+
class Solution:
74+
def reverseWords(self, s: str) -> str:
75+
words = s.strip().split()
76+
return ' '.join(words[::-1])
7477
```
7578

7679
### **Java**
7780

7881
```java
82+
class Solution {
83+
public String reverseWords(String s) {
84+
List<String> words = Arrays.asList(s.trim().split("\\s+"));
85+
Collections.reverse(words);
86+
return String.join(" ", words);
87+
}
88+
}
89+
```
90+
91+
### **C#**
7992

93+
```cs
94+
public class Solution {
95+
public string ReverseWords(string s) {
96+
return string.Join(" ", s.Trim().Split(" ").Where(word => !string.IsNullOrEmpty(word) && !string.IsNullOrEmpty(word.Trim())).Reverse());
97+
}
98+
}
8099
```
81100

82101
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,5 @@
1-
using System.Text;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
51
public class Solution {
62
public string ReverseWords(string s) {
7-
var sb = new StringBuilder();
8-
var wordList = new List<string>();
9-
foreach (var ch in s)
10-
{
11-
if (char.IsWhiteSpace(ch))
12-
{
13-
if (sb.Length > 0)
14-
{
15-
wordList.Add(sb.ToString());
16-
sb.Clear();
17-
}
18-
}
19-
else
20-
{
21-
sb.Append(ch);
22-
}
23-
}
24-
if (sb.Length > 0)
25-
{
26-
wordList.Add(sb.ToString());
27-
sb.Clear();
28-
}
29-
30-
foreach (var word in ((IEnumerable<string>)wordList).Reverse())
31-
{
32-
if (sb.Length > 0)
33-
{
34-
sb.Append(' ');
35-
}
36-
sb.Append(word);
37-
}
38-
39-
return sb.ToString();
3+
return string.Join(" ", s.Trim().Split(" ").Where(word => !string.IsNullOrEmpty(word) && !string.IsNullOrEmpty(word.Trim())).Reverse());
404
}
415
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
11
class Solution {
22
public String reverseWords(String s) {
3-
int length = s.length();
4-
if(length ==0)return s;
5-
char[] res=new char[length];
6-
int len=helper(s.toCharArray(), length -1,res,0,0);
7-
return new String(res,0,len);
8-
}
9-
private int helper(char[] ch,int r,char[] res,int l,int len){
10-
while(r>=0&&ch[r]==' ') r--;
11-
if(r<0)return Math.max(0,len-1);
12-
int rigth=r;
13-
while(r>=0&&ch[r]!=' ') r--;
14-
len+=rigth-r+1;
15-
for(int left=r+1;left<=rigth;left++,l++) res[l] = ch[left];
16-
if(l<res.length) res[l++] = ' ';
17-
return helper(ch,r,res,l,len);
3+
List<String> words = Arrays.asList(s.trim().split("\\s+"));
4+
Collections.reverse(words);
5+
return String.join(" ", words);
186
}
197
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def reverseWords(self, s: str) -> str:
3+
words = s.strip().split()
4+
return ' '.join(words[::-1])

0 commit comments

Comments
 (0)