Leetcode - Sort Colors

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:

Paste_Image.png

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

通过设置头尾指针,来将三种颜色分类。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的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,779评论 0 33
  • My code: 这道题目我没有做出来。应该说,我做出来了,但是超时了。我也想到了用dp,但是用的是二维数组。后来...
    Richardo92阅读 531评论 0 0
  • 谈恋爱之前,对方也许不是喜欢某一个人,而是喜欢一类人, 没有谁是不可替代的。 你凭借姣好的脸蛋闯入男人的视线, 企...
    霓鲤阅读 621评论 0 0
  • 自控力,在我的概念里,这是一种非常强的能力,能做到自控的人肯定是一个强人,因为我的自控能力就非常的差,所以我有很严...
    丁丁加油阅读 233评论 0 1
  • 偶尔充当知心姐姐看到你们在留言板的心酸还有对我的想念,害怕与一路走来一直念叨着我的你们失去联系,却有着一颗想要和过...
    猫你阅读 503评论 0 0