import java.util.Arrays;

/**
* Source : https://oj.leetcode.com/problems/sort-colors/
*
*
* 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.
*
* Note:
* You are not suppose to use the library's sort function for this problem.
*
* Follow up:
* > 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?
*/
public class SortColors { /**
* 数组由三种颜色组成,将三种颜色归类排序,使相同的颜色紧邻,本题目有以下特点
* 数组由三种颜色构成,分别用0,1,2代替
*
* 题目中提示已经说明,一种直接的办法就是遍历数组两次,分别对两种颜色排序
*
* 但是能不能用一次遍历,占用常数空间来完成呢?
* 利用数组只由0,1,2构成的特性,只要对个数字排序,另一个自然也就是有序的了,可以遍历一次数组,维护两个下标,left和right,
* 从数组两头开始,left记录0的位置,right记录2的位置
*
* @param arr
*/
public void sort (int[] arr) {
int left = 0;
int right = arr.length - 1;
int i = 0;
while (i < right) {
if (arr[i] == 0) {
swap(arr, i++, left++);
} else if (arr[i] == 2) {
swap(arr, i, right--);
} else {
i++;
}
}
} private void swap (int[] arr, int left, int right) {
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
} public static void main(String[] args) {
SortColors sortColors = new SortColors();
int[] arr = new int[]{};
int[] arr1 = new int[]{0};
int[] arr2 = new int[]{0,1,2};
int[] arr3 = new int[]{0,0,1,1,1,2,2};
int[] arr4 = new int[]{1,2,0,0,1,1,1,2,2,0,1}; sortColors.sort(arr);
sortColors.sort(arr1);
sortColors.sort(arr2);
sortColors.sort(arr3);
sortColors.sort(arr4);
System.out.println(Arrays.toString(arr));
System.out.println(Arrays.toString(arr1));
System.out.println(Arrays.toString(arr2));
System.out.println(Arrays.toString(arr3));
System.out.println(Arrays.toString(arr4)); }
}

leetcode — sort-colors的更多相关文章

  1. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  2. [LeetCode] Sort Colors 颜色排序

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  3. [leetcode]Sort Colors @ Python

    原题地址:https://oj.leetcode.com/problems/sort-colors/ 题意: Given an array with n objects colored red, wh ...

  4. [Leetcode] Sort Colors (C++)

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  5. 75.[LeetCode] Sort Colors

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  6. [LeetCode] Sort Colors 对于元素取值有限的数组,只遍历一遍的排序方法

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  7. [LeetCode] Sort Colors 只有3个类型的排序

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  8. LeetCode Sort Colors (技巧)

    题意: 一个数组只可能含有3种数据,分别为1,2,3,请将数组排序(只能扫一遍). 思路: 如果扫两遍的话,用个桶记录一下,再赋值上去就行了. class Solution { public: voi ...

  9. 【LeetCode】Sort Colors 数组排序

    题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...

  10. LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors

    1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...

随机推荐

  1. Python Day 12

    阅读目录: 内容回顾 函数默认值的细节 三元表达式 列表与字典推导式 函数对象 名称空间 函数嵌套的定义 作用域 ##内容回顾 # 字符串的比较 -- 按照从左往右比较每一个字符,通过字符对应的asc ...

  2. Note of Python Math

    Note of Python Math math 库是Python 提供的内置数学类函数库,而其中复数类型常用于科学计算,一般计算并不常用,因此math 库不支持复数类型.math 库一共提供4个数学 ...

  3. idea 中dao层自动生成接口

    1.在生成接口的类上右键 2.选中要生成的接口方法 3.点击Yes 4.出现(? reference in ? file)即生成成功

  4. css3等待框

    第1种效果: <div class="loading"> <span></span> <span></span> < ...

  5. Teradata Delete Database and Drop Database

    DELETE DATABASE and DELETE USER statements delete all data tables, views, and macros from a database ...

  6. 我的C#跨平台之旅(四):使用AOP(filter、attribute)进行系统增强

    1.使用OData提速REST API开发 引入NuGet包:Microsoft.AspNet.WebApi.OData 在启动类中添加如下配置(示例为全局配置,也可基于Controller或Acti ...

  7. hive -help hive命令行执行sql参数

    在shell命令行执行 hive -help 结果如下: -d,--define <key=value> Variable substitution to apply to Hive co ...

  8. 《HTTP权威指南》6-代理

    Web的中间实体 HTTP的代理服务器既是Web服务器又是Web客户端,它既需要正确地处理从客户端发来的请求和连接,返回响应,有需要向服务器发送请求,并接受响应. 私有和共享代理 代理服务器可以是某个 ...

  9. Java并发编程实战

    代码中比较容易出现bug的场景: 不一致的同步,直接调用Thread.run,未被释放的锁,空的同步块,双重检查加锁,在构造函数中启动一个线程,notify或notifyAll通知错误,Object. ...

  10. nginx 开启gzip 压缩资源

    upstream sems { server 127.0.0.1:10171 weight=1 fail_timeout=0; } server { listen 80; server_name ww ...