博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LintCode/LeetCode] Sort Colors I & II
阅读量:6088 次
发布时间:2019-06-20

本文共 2491 字,大约阅读时间需要 8 分钟。

Sort Color I

Problem

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.

Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

Notice

You are not suppose to use the library's sort function for this problem.

You should do it in-place (sort numbers in the original array).

Example

Given [1, 0, 1, 2], sort it in-place to [0, 1, 1, 2].

Challenge

A rather straight forward solution is a two-pass algorithm using counting sort.

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.

Could you come up with an one-pass algorithm using only constant space?

Note

第一题可以使用双指针法,构造swap函数,将0换到左边,将2换到右边,遇到1跳过。

另一种方法如下所示,构造色彩数组。然后对nums重新分配数值。

Solution

Update 2018-9

class Solution {    public void sortColors(int[] nums) {        if (nums == null || nums.length < 2) return;        int m = 0, n = nums.length-1; //m is index of 0's, n is index of 2's        for (int i = 0; i <= n; i++) {            if (nums[i] == 0) swap(nums, i, m++);            else if (nums[i] == 2) swap(nums, i--, n--);        }    }    private void swap(int[] nums, int i, int j) {        int temp = nums[i];        nums[i] = nums[j];        nums[j] = temp;    }}

2016 version

class Solution {    public void sortColors(int[] nums) {        int[] count = new int[3];        for (int num: nums) {            count[num]++;        }        int pos = 0;        for (int i = 0; i < 3; i++) {            while (count[i] > 0) {                nums[pos++] = i;                count[i]--;            }        }        return ;    }}

Sort Colors II

Problem

Given an array of n objects with k different colors (numbered from 1 to k), sort them so that objects of the same color are adjacent, with the colors in the order 1, 2, ... k.

Notice

You are not suppose to use the library's sort function for this problem.

Example

Given colors=[3, 2, 2, 1, 4], k=4, your code should sort colors in-place to [1, 2, 2, 3, 4].

Note

Sort Color I的做法一样。

Solution

class Solution {    public void sortColors2(int[] colors, int k) {        int[] count = new int[k + 1];        for (int i : colors) {            count[i]++;        }        int pos = 0;        for (int i = 1; i <= k; i++) {            while (count[i] > 0) {                colors[pos++] = i;                count[i]--;            }        }    }}

转载地址:http://vqvwa.baihongyu.com/

你可能感兴趣的文章
怎样才能在MathType中快速引用公式编号
查看>>
_BSMachError: (os/kern) invalid name (15)
查看>>
IDEA14.1 console log4j utf-8乱码
查看>>
SQL使用case when 动态的修改where条件
查看>>
[leetcode] Longest Valid Parentheses
查看>>
fork与Vfork比较
查看>>
java内存管理机制及内存区域详解
查看>>
Java 将PDF转图片上传到FTP上
查看>>
javascript-closure
查看>>
基于jpa的一对一的主键测试
查看>>
使用cfx与springMVC集成发布与调用webservice
查看>>
Java强弱引用示例
查看>>
SpringMVC常用注解标签详解
查看>>
[NBOJ0031][Nim-B* Sum]
查看>>
[lnmp]lnmp集成安装包关于graphviz安装(failed to execute cmd
查看>>
Android smali文件,jar包字符串混淆
查看>>
centos安装教程
查看>>
kafka 扩容之后迁移topic
查看>>
CYQ.Data 轻量数据层之路 常见问题QA(三十)
查看>>
秋天新作品:秋式广告杀手,每年帮大伙多活7-8天!
查看>>