Skip to content

Commit e196403

Browse files
committed
0075. Sort Colors
1 parent 559d1d6 commit e196403

File tree

4 files changed

+190
-0
lines changed

4 files changed

+190
-0
lines changed

markdown/0075. Sort Colors.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
### [75\. Sort Colors](https://leetcode.com/problems/sort-colors/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given an array with _n_ objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
7+
8+
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
9+
10+
**Note:** You are not suppose to use the library's sort function for this problem.
11+
12+
**Example:**
13+
14+
```
15+
Input: [2,0,2,1,1,0]
16+
Output: [0,0,1,1,2,2]
17+
```
18+
19+
**Follow up:**
20+
21+
* A rather straight forward solution is a two-pass algorithm using counting sort.
22+
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.
23+
* Could you come up with a one-pass algorithm using only constant space?
24+
25+
26+
#### Solution
27+
28+
Language: **Java**
29+
30+
```java
31+
class Solution {
32+
   public void sortColors(int[] nums) { // 搞个堆排序吧
33+
       // 第一步,build heap
34+
       for (int i = nums.length / 2 - 1; i >= 0; i--) {
35+
           heapify(nums, i, nums.length);
36+
      }
37+
       // 第二步, heap sort
38+
       for (int i = nums.length - 1; i > 0; i--) {
39+
           swap(nums, 0, i);
40+
           heapify(nums, 0, i);
41+
      }
42+
  }
43+
44+
   // 这里第三个参数的目的是在堆排序阶段,最后面的值是已经排序出来的,因此不参与 heapify 了
45+
   private void heapify(int[] nums, int index, int length) {
46+
       int lc = index * 2 + 1;
47+
       int rc = lc + 1;
48+
       int maxc = lc;
49+
       if (lc >= length) {
50+
           return;
51+
      }
52+
       if (rc < length && nums[lc] < nums[rc]) {
53+
           maxc = rc;
54+
      }
55+
       if (nums[maxc] > nums[index]) {
56+
           swap(nums, index, maxc);
57+
           this.heapify(nums, maxc, length);
58+
      }
59+
  }
60+
61+
   private void swap(int[] nums, int x, int y) {
62+
       int temp = nums[x];
63+
       nums[x] = nums[y];
64+
       nums[y] = temp;
65+
  }
66+
}
67+
```
68+
![pic](https://gitee.com/jacobchang/PicBed/raw/master/srV1na.png)

src/main/java/leetcode/_75_/Main.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package leetcode._75_;
2+
3+
import leetcode.common.Printer;
4+
5+
/**
6+
* Created by zhangbo54 on 2019-03-04.
7+
*/
8+
public class Main {
9+
public static void main(String[] args) {
10+
Solution solution = new Solution();
11+
int[] nums = {2, 0, 2, 1, 1, 0};
12+
solution.sortColors(nums);
13+
Printer.printArrays(nums);
14+
}
15+
}
16+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package leetcode._75_;
2+
3+
class Solution {
4+
public void sortColors(int[] nums) { // 搞个堆排序吧
5+
// 第一步,build heap
6+
for (int i = nums.length / 2 - 1; i >= 0; i--) {
7+
heapify(nums, i, nums.length);
8+
}
9+
// 第二步, heap sort
10+
for (int i = nums.length - 1; i > 0; i--) {
11+
swap(nums, 0, i);
12+
heapify(nums, 0, i);
13+
}
14+
}
15+
16+
// 这里第三个参数的目的是在堆排序阶段,最后面的值是已经排序出来的,因此不参与 heapify 了
17+
private void heapify(int[] nums, int index, int length) {
18+
int lc = index * 2 + 1;
19+
int rc = lc + 1;
20+
int maxc = lc;
21+
if (lc >= length) {
22+
return;
23+
}
24+
if (rc < length && nums[lc] < nums[rc]) {
25+
maxc = rc;
26+
}
27+
if (nums[maxc] > nums[index]) {
28+
swap(nums, index, maxc);
29+
this.heapify(nums, maxc, length);
30+
}
31+
}
32+
33+
private void swap(int[] nums, int x, int y) {
34+
int temp = nums[x];
35+
nums[x] = nums[y];
36+
nums[y] = temp;
37+
}
38+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
### [75\. Sort Colors](https://leetcode.com/problems/sort-colors/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given an array with _n_ objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
7+
8+
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
9+
10+
**Note:** You are not suppose to use the library's sort function for this problem.
11+
12+
**Example:**
13+
14+
```
15+
Input: [2,0,2,1,1,0]
16+
Output: [0,0,1,1,2,2]
17+
```
18+
19+
**Follow up:**
20+
21+
* A rather straight forward solution is a two-pass algorithm using counting sort.
22+
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.
23+
* Could you come up with a one-pass algorithm using only constant space?
24+
25+
26+
#### Solution
27+
28+
Language: **Java**
29+
30+
```java
31+
class Solution {
32+
   public void sortColors(int[] nums) { // 搞个堆排序吧
33+
       // 第一步,build heap
34+
       for (int i = nums.length / 2 - 1; i >= 0; i--) {
35+
           heapify(nums, i, nums.length);
36+
      }
37+
       // 第二步, heap sort
38+
       for (int i = nums.length - 1; i > 0; i--) {
39+
           swap(nums, 0, i);
40+
           heapify(nums, 0, i);
41+
      }
42+
  }
43+
44+
   // 这里第三个参数的目的是在堆排序阶段,最后面的值是已经排序出来的,因此不参与 heapify 了
45+
   private void heapify(int[] nums, int index, int length) {
46+
       int lc = index * 2 + 1;
47+
       int rc = lc + 1;
48+
       int maxc = lc;
49+
       if (lc >= length) {
50+
           return;
51+
      }
52+
       if (rc < length && nums[lc] < nums[rc]) {
53+
           maxc = rc;
54+
      }
55+
       if (nums[maxc] > nums[index]) {
56+
           swap(nums, index, maxc);
57+
           this.heapify(nums, maxc, length);
58+
      }
59+
  }
60+
61+
   private void swap(int[] nums, int x, int y) {
62+
       int temp = nums[x];
63+
       nums[x] = nums[y];
64+
       nums[y] = temp;
65+
  }
66+
}
67+
```
68+
![pic](https://gitee.com/jacobchang/PicBed/raw/master/srV1na.png)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy