Leetcode - Sort Colors

Leetcode - Sort Colors_第1张图片

My code:

public class Solution {
    public void sortColors(int[] nums) {
        if (nums == null || nums.length == 0)
            return;
        int head = 0;
        int tail = nums.length - 1;
        for (int i = 0; i < nums.length; i++) {
            if (i > tail)
                return;
            if (nums[i] == 2) {
                int j = tail;
                while (j > i && nums[j] == 2)
                    j--;
                if (j == i)
                    return;
                nums[i] = nums[j];
                nums[j] = 2;
                tail = j - 1;
                i--;
            }
            else if (nums[i] == 0) {
                nums[i] = 1;
                nums[head] = 0;
                head++;
            }
            else
                continue;
        }
    }
}

My test result:

Leetcode - Sort Colors_第2张图片
Paste_Image.png

这道题目还是比较简单的。具体看我的画图分析。

Leetcode - Sort Colors_第3张图片

通过设置头尾指针,来将三种颜色分类。0 的话往前移,2的往后 移。1的直接跳过。

**
总结: Array, Two pointer
**

Anyway, Good luck, Richardo!

My code:

public class Solution {
    public void sortColors(int[] nums) {
        if (nums == null || nums.length == 0)
            return;
        int i = 0;
        int j = nums.length - 1;
        int scan = 0;
        while (scan <= j) {
            if (nums[scan] == 0) {
                nums[scan] = nums[i];
                nums[i] = 0;
                i++;
                scan++;
            }
            else if (nums[scan] == 1) {
                scan++;
            }
            else {
                nums[scan] = nums[j];
                nums[j] = 2;
                j--;
            }
        }
    }
}

感觉我第二遍写的代码比第一遍简单很多啊。
就是碰到1就往前走。如果碰到了0,就和i换,然后i++,scan++
如果碰到了2,就和j换,j--

Anyway, Good luck, Richardo!

My code:

public class Solution {
    public void sortColors(int[] nums) {
        if (nums == null || nums.length == 0) {
            return;
        }
        
        int begin = 0;
        int i = 0;
        int end = nums.length - 1;
        
        while (i <= end) {
            if (nums[i] == 0) {
                nums[i] = nums[begin];
                nums[begin] = 0;
                begin++;
                i++;
            }
            else if (nums[i] == 1) {
                i++;
            }
            else {
                nums[i] = nums[end];
                nums[end] = 2;
                end--;
            }
        }
    }
}

跟快速排序+考虑重复元素的算法差不多,三个指针解决。

Anyway, Good luck, Richardo! -- 08/07/2016

看到了一种全新的解法,而且不用swap
My code:

public class Solution {
    public void sortColors(int[] nums) {
        if (nums == null || nums.length == 0) {
            return;
        }
        
        int red = 0;
        int white = 0;
        for (int i = 0; i < nums.length; i++) {
            int temp = nums[i];
            nums[i] = 2;
            if (temp < 2) {
                nums[white] = 1;
                white++;
            }
            if (temp == 0) {
                nums[red] = 0;
                red++;
            }
        }        
    }
}

Anyway, Good luck, Richardo! -- 09/04/2016

你可能感兴趣的:(Leetcode - Sort Colors)