File tree 2 files changed +45
-2
lines changed 2 files changed +45
-2
lines changed Original file line number Diff line number Diff line change 1
1
class Solution (object ):
2
2
3
-
4
-
5
3
def reverseWords (self , s ):
6
4
7
5
return " " .join (x [::- 1 ] for x in s .split (" " ))
Original file line number Diff line number Diff line change
1
+ struct Solution ( ) ;
2
+ impl Solution {
3
+ pub fn reverse_words ( s : String ) -> String {
4
+
5
+ let mut v = s. into_bytes ( ) ;
6
+ let ( mut left, mut right) = ( 0 , 0 ) ;
7
+
8
+ while right < v. len ( ) {
9
+ while right < v. len ( ) && v[ right] != ' ' as u8 {
10
+ right += 1
11
+ }
12
+
13
+ let next = right + 1 ;
14
+
15
+ while left < right {
16
+ unsafe {
17
+ std:: ptr:: swap ( & mut v[ right -1 ] , & mut v[ left] ) ;
18
+ }
19
+ left += 1 ;
20
+ right -= 1 ;
21
+ }
22
+
23
+ left = next;
24
+ right = next;
25
+ }
26
+
27
+ String :: from_utf8 ( v) . unwrap ( )
28
+ }
29
+ }
30
+
31
+ #[ cfg( test) ]
32
+ mod tests {
33
+ use super :: * ;
34
+
35
+ #[ test] fn test_1 ( ) {
36
+
37
+
38
+
39
+ assert_eq ! (
40
+ Solution :: reverse_words( String :: from( "Let's take LeetCode contest" ) ) ,
41
+ String :: from( "s'teL ekat edoCteeL tsetnoc" ) )
42
+ }
43
+
44
+
45
+ }
You can’t perform that action at this time.
0 commit comments