|
| 1 | +package com.diguage.algorithm.leetcode; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.Objects; |
| 5 | + |
| 6 | +/** |
| 7 | + * = 75. Sort Colors |
| 8 | + * |
| 9 | + * https://leetcode.com/problems/sort-colors/[Sort Colors - LeetCode] |
| 10 | + * |
| 11 | + * Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue. |
| 12 | + * |
| 13 | + * Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively. |
| 14 | + * |
| 15 | + * Note: You are not suppose to use the library's sort function for this problem. |
| 16 | + * |
| 17 | + * .Example: |
| 18 | + * [source] |
| 19 | + * ---- |
| 20 | + * Input: [2,0,2,1,1,0] |
| 21 | + * Output: [0,0,1,1,2,2] |
| 22 | + * ---- |
| 23 | + * |
| 24 | + * *Follow up:* |
| 25 | + * |
| 26 | + * * A rather straight forward solution is a two-pass algorithm using counting sort. |
| 27 | + * + |
| 28 | + * 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. |
| 29 | + * + |
| 30 | + * * Could you come up with a one-pass algorithm using only constant space? |
| 31 | + * |
| 32 | + * @author D瓜哥, https://www.diguage.com/ |
| 33 | + * @since 2019-10-29 01:01 |
| 34 | + */ |
| 35 | +public class SortColors { |
| 36 | + /** |
| 37 | + * Runtime: 0 ms, faster than 100.00% of Java online submissions for Sort Colors. |
| 38 | + * |
| 39 | + * Memory Usage: 35.2 MB, less than 99.21% of Java online submissions for Sort Colors. |
| 40 | + */ |
| 41 | + public void sortColors(int[] nums) { |
| 42 | + if (Objects.isNull(nums) || nums.length <= 1) { |
| 43 | + return; |
| 44 | + } |
| 45 | + int current = 0; |
| 46 | + int head = 0; |
| 47 | + int tail = nums.length - 1; |
| 48 | + while (current <= tail) { |
| 49 | + if (nums[current] == 0) { |
| 50 | + swap(nums, current, head); |
| 51 | + head++; |
| 52 | + current++; |
| 53 | + } else if (nums[current] == 2) { |
| 54 | + swap(nums, current, tail); |
| 55 | + tail--; |
| 56 | + // 这了不需要 current++,就是把这个数字重新排队一遍! |
| 57 | + } else { |
| 58 | + current++; |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private void swap(int[] nums, int i, int j) { |
| 64 | + int temp = nums[i]; |
| 65 | + nums[i] = nums[j]; |
| 66 | + nums[j] = temp; |
| 67 | + } |
| 68 | + |
| 69 | + public static void main(String[] args) { |
| 70 | + SortColors solution = new SortColors(); |
| 71 | + int[] array = {2, 0, 2, 1, 1, 0}; |
| 72 | + System.out.println(Arrays.toString(array)); |
| 73 | + solution.sortColors(array); |
| 74 | + System.out.println(Arrays.toString(array)); |
| 75 | + |
| 76 | + int[] array2 = {1, 2, 0, 0}; |
| 77 | + System.out.println(Arrays.toString(array2)); |
| 78 | + solution.sortColors(array2); |
| 79 | + System.out.println(Arrays.toString(array2)); |
| 80 | + |
| 81 | + int[] array3 = {1, 2, 2, 2, 2, 0, 0, 0, 1, 1}; |
| 82 | + System.out.println(Arrays.toString(array3)); |
| 83 | + solution.sortColors(array3); |
| 84 | + System.out.println(Arrays.toString(array3)); |
| 85 | + } |
| 86 | +} |
0 commit comments