File tree 2 files changed +36
-1
lines changed
2 files changed +36
-1
lines changed Original file line number Diff line number Diff line change 2
2
#include <stdlib.h>
3
3
#include <string.h>
4
4
5
+
5
6
static inline void swap (int * a , int * b )
6
7
{
7
8
int tmp = * a ;
8
9
* a = * b ;
9
10
* b = tmp ;
10
11
}
11
12
12
- static void sortColors (int * nums , int numsSize )
13
+ void sortColors (int * nums , int numsSize )
13
14
{
14
15
int i , j = 0 ;
15
16
for (i = 0 ; i < numsSize ; i ++ ) {
Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+
3
+ using namespace std ;
4
+
5
+ class Solution {
6
+ public:
7
+ void sortColors (vector<int >& nums) {
8
+ int i = 0 , j = nums.size () - 1 ;
9
+ while (i < j) {
10
+ if (nums[i] == 0 ) {
11
+ i++;
12
+ continue ;
13
+ }
14
+ if (nums[j] != 0 ) {
15
+ j--;
16
+ continue ;
17
+ }
18
+ swap (nums[i], nums[j]);
19
+ }
20
+
21
+ j = nums.size () - 1 ;
22
+ while (i < j) {
23
+ if (nums[i] == 1 ) {
24
+ i++;
25
+ continue ;
26
+ }
27
+ if (nums[j] != 1 ) {
28
+ j--;
29
+ continue ;
30
+ }
31
+ swap (nums[i], nums[j]);
32
+ }
33
+ }
34
+ };
You can’t perform that action at this time.
0 commit comments