51
51
52
52
<!-- solution:start -->
53
53
54
- ### Solution 1
54
+ ### Solution 1: Simulation
55
+
56
+ We can split the string $\textit{s}$ into an array of words $\textit{words}$ by spaces, then reverse each word and concatenate them back into a string.
57
+
58
+ The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string $\textit{s}$.
55
59
56
60
<!-- tabs:start -->
57
61
@@ -60,22 +64,19 @@ tags:
60
64
``` python
61
65
class Solution :
62
66
def reverseWords (self , s : str ) -> str :
63
- return ' ' .join([ t[::- 1 ] for t in s.split(' ' )] )
67
+ return " " .join(t[::- 1 ] for t in s.split() )
64
68
```
65
69
66
70
#### Java
67
71
68
72
``` java
69
73
class Solution {
70
74
public String reverseWords (String s ) {
71
- StringBuilder res = new StringBuilder ();
72
- for (String t : s. split(" " )) {
73
- for (int i = t. length() - 1 ; i >= 0 ; -- i) {
74
- res. append(t. charAt(i));
75
- }
76
- res. append(" " );
75
+ String [] words = s. split(" " );
76
+ for (int i = 0 ; i < words. length; ++ i) {
77
+ words[i] = new StringBuilder (words[i]). reverse(). toString();
77
78
}
78
- return res . substring( 0 , res . length() - 1 );
79
+ return String . join( " " , words );
79
80
}
80
81
}
81
82
```
@@ -86,14 +87,16 @@ class Solution {
86
87
class Solution {
87
88
public:
88
89
string reverseWords(string s) {
89
- for (int i = 0, n = s.size(); i < n; ++i) {
90
- int j = i;
91
- while (++j < n && s[ j] != ' ')
92
- ;
93
- reverse(s.begin() + i, s.begin() + j);
94
- i = j;
90
+ stringstream ss(s);
91
+ string t;
92
+ string ans;
93
+ while (ss >> t) {
94
+ reverse(t.begin(), t.end());
95
+ ans += t;
96
+ ans.push_back(' ');
95
97
}
96
- return s;
98
+ ans.pop_back();
99
+ return ans;
97
100
}
98
101
};
99
102
```
@@ -102,18 +105,13 @@ public:
102
105
103
106
```go
104
107
func reverseWords(s string) string {
105
- t := []byte(s)
106
- for i := 0; i < len(t); i++ {
107
- j := i
108
- for j < len(t) && t[j] != ' ' {
109
- j++
110
- }
111
- for st, ed := i, j-1; st < ed; st, ed = st+1, ed-1 {
112
- t[st], t[ed] = t[ed], t[st]
113
- }
114
- i = j
108
+ words := strings.Fields(s)
109
+ for i, w := range words {
110
+ t := []byte(w)
111
+ slices.Reverse(t)
112
+ words[i] = string(t)
115
113
}
116
- return string(t )
114
+ return strings.Join(words, " " )
117
115
}
118
116
```
119
117
@@ -122,14 +120,8 @@ func reverseWords(s string) string {
122
120
``` ts
123
121
function reverseWords(s : string ): string {
124
122
return s
125
- .split (/ \s + / )
126
- .map (str => {
127
- let res = ' ' ;
128
- for (const c of str ) {
129
- res = c + res ;
130
- }
131
- return res ;
132
- })
123
+ .split (' ' )
124
+ .map (t => t .split (' ' ).reverse ().join (' ' ))
133
125
.join (' ' );
134
126
}
135
127
```
@@ -147,6 +139,21 @@ impl Solution {
147
139
}
148
140
```
149
141
142
+ #### JavaScript
143
+
144
+ ``` js
145
+ /**
146
+ * @param {string} s
147
+ * @return {string}
148
+ */
149
+ var reverseWords = function (s ) {
150
+ return s
151
+ .split (' ' )
152
+ .map (t => t .split (' ' ).reverse ().join (' ' ))
153
+ .join (' ' );
154
+ };
155
+ ```
156
+
150
157
#### PHP
151
158
152
159
``` php
@@ -156,11 +163,11 @@ class Solution {
156
163
* @return String
157
164
*/
158
165
function reverseWords($s) {
159
- $sArr = explode(' ', $s);
160
- for ($i = 0; $i < count($sArr); $i++ ) {
161
- $sArr [$i] = strrev($sArr[$i] );
166
+ $words = explode(' ', $s);
167
+ foreach ($words as $i => $word ) {
168
+ $words [$i] = strrev($word );
162
169
}
163
- return implode(' ', $sArr );
170
+ return implode(' ', $words );
164
171
}
165
172
}
166
173
```
0 commit comments