forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_75.java
More file actions
26 lines (23 loc) · 679 Bytes
/
_75.java
File metadata and controls
26 lines (23 loc) · 679 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.fishercoder.solutions;
public class _75 {
public static class Solution1 {
public void sortColors(int[] nums) {
int zero = 0;
int two = nums.length - 1;
for (int i = 0; i <= two; ) {
if (nums[i] == 0 && i > zero) {
swap(nums, i, zero++);
} else if (nums[i] == 2 && i < two) {
swap(nums, i, two--);
} else {
i++;
}
}
}
void swap(int[] nums, int m, int n) {
int temp = nums[m];
nums[m] = nums[n];
nums[n] = temp;
}
}
}