File tree Expand file tree Collapse file tree 2 files changed +36
-1
lines changed Expand file tree Collapse file tree 2 files changed +36
-1
lines changed Original file line number Diff line number Diff line change 22#include <stdlib.h>
33#include <string.h>
44
5+
56static inline void swap (int * a , int * b )
67{
78 int tmp = * a ;
89 * a = * b ;
910 * b = tmp ;
1011}
1112
12- static void sortColors (int * nums , int numsSize )
13+ void sortColors (int * nums , int numsSize )
1314{
1415 int i , j = 0 ;
1516 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