Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
34 lines (32 loc) · 1.19 KB

File metadata and controls

34 lines (32 loc) · 1.19 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
27
28
29
30
31
32
33
34
package ch17;
public class P69_1 {
public void sortColors(int[] nums) {
// red와 blue는 양쪽 끝 포인터이며, white는 중앙에서 움직이는 포인터다.
int red = 0;
int white = 0;
int blue = nums.length;
// 두 값이 역전될 때까지 반복
while (white < blue) {
// white 포인터가 중앙값보다 작다면
if (nums[white] < 1) {
// red와 white 스왑
int temp = nums[red];
nums[red] = nums[white];
nums[white] = temp;
// red와 white 포인터는 한 칸씩 우측으로 이동
red++;
white++;
} else if (nums[white] > 1) { // white 포인터가 중앙값보다 크다면
// blue 포인터 좌측으로 이동
blue--;
// white와 blue 스왑
int temp = nums[white];
nums[white] = nums[blue];
nums[blue] = temp;
} else {
// 값이 같은 경우이므로, 스왑 없이 white 포인터만 우측으로 이동
white++;
}
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.