File tree 5 files changed +51
-10
lines changed
5 files changed +51
-10
lines changed Original file line number Diff line number Diff line change @@ -170,10 +170,8 @@ impl Solution {
170
170
let mut l = 0 ;
171
171
let mut r = nums . len () - 1 ;
172
172
while l < r {
173
- let num = nums [l ];
174
- if num % 2 == 0 {
175
- nums [l ] = nums [r ];
176
- nums [r ] = num ;
173
+ if nums [l ] % 2 == 0 {
174
+ nums . swap (l , r );
177
175
r -= 1 ;
178
176
} else {
179
177
l += 1 ;
Original file line number Diff line number Diff line change @@ -6,10 +6,8 @@ impl Solution {
6
6
let mut l = 0 ;
7
7
let mut r = nums. len ( ) - 1 ;
8
8
while l < r {
9
- let num = nums[ l] ;
10
- if num % 2 == 0 {
11
- nums[ l] = nums[ r] ;
12
- nums[ r] = num;
9
+ if nums[ l] % 2 == 0 {
10
+ nums. swap ( l, r) ;
13
11
r -= 1 ;
14
12
} else {
15
13
l += 1 ;
Original file line number Diff line number Diff line change 36
36
- 存在,即 ` return ` 返回。
37
37
- 不存在,记录元素,继续遍历。
38
38
39
- _ 复杂度 _ :
39
+ * 复杂度 * :
40
40
41
41
- 时间 ** _ O(N)_ **
42
42
- 空间 ** _ O(N)_ **
52
52
53
53
> 因为数组是有序的,指针变动对值的影响可预测。
54
54
55
- _ 复杂度 _ :
55
+ * 复杂度 * :
56
56
57
57
- 时间 ** _ O(N)_ **
58
58
- 空间 ** _ O(1)_ **
Original file line number Diff line number Diff line change @@ -79,6 +79,31 @@ class Solution {
79
79
}
80
80
```
81
81
82
+ ### ** C++**
83
+
84
+ ``` cpp
85
+ class Solution {
86
+ public:
87
+ string reverseWords(string s) {
88
+ string res;
89
+ int i = s.size() - 1;
90
+ while (i >= 0) {
91
+ if (s[ i] == ' ') {
92
+ i--;
93
+ } else {
94
+ int j = i;
95
+ while (i >= 0 && s[ i] != ' ') {
96
+ i--;
97
+ }
98
+ res += s.substr(i + 1, j - i);
99
+ res.push_back(' ');
100
+ }
101
+ }
102
+ return res.substr(0, res.size() - 1);
103
+ }
104
+ };
105
+ ```
106
+
82
107
### **JavaScript**
83
108
84
109
```js
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ string reverseWords (string s) {
4
+ string res;
5
+ int i = s.size () - 1 ;
6
+ while (i >= 0 ) {
7
+ if (s[i] == ' ' ) {
8
+ i--;
9
+ } else {
10
+ int j = i;
11
+ while (i >= 0 && s[i] != ' ' ) {
12
+ i--;
13
+ }
14
+ res += s.substr (i + 1 , j - i);
15
+ res.push_back (' ' );
16
+ }
17
+ }
18
+ return res.substr (0 , res.size () - 1 );
19
+ }
20
+ };
You can’t perform that action at this time.
0 commit comments