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. EFLinq查询

    1.无参数查询var model = db.Database.SqlQuery<UserInfo>("select* from UserInfoes ").ToList ...

  2. 关于PHP读取HTTP头的部分

    本文转载自https://my.oschina.net/luoczi/blog/86608 1.关于PHP读取HTTP头的方法 $_SERVER['PHP_SELF'] #当前正在执行脚本的文件名,与 ...

  3. JS prototype chaining(原型链)整理中······

    初学原型链整理 构造器(constructor).原型(prototype).实例(instance); 每一个构造器都有一个prototype对象,这个prototype对象有一个指针指向该构造器: ...

  4. ArcSDE

    ArcSDE,即数据通路,是ArcGIS的空间数据引擎,它是在关系数据库管理系统(RDBMS)中存储和管理多用户空间数据库的通路.从空间数据管理的角度看,ArcSDE是一个连续的空间数据模型,借助这一 ...

  5. 《Serverless架构-无服务单页应用开发》读后感

    本书的作者是[美]Ben Rady,译者郑赞美.简传挺,书中作者详细的介绍了如何使用html.js以及amazon提供的诸多云服务(Simple Storage Service(S3).Cognito ...

  6. 万里长征第一步:Python进程池的一点点小坑

    # -*- coding: utf- -*- """ Created on Thu Mar :: @author: lilide """ # ...

  7. Il laser che è chiaramente visibile

    Prima di quel tempo ho ottenuto questo potente puntatore laser 500mW, non so davvero come questo dis ...

  8. day23_雷神_git

    git 版本控制工具 下载: https://git-scm.com/ git clone: 是用来拉代码的,git pull是更新当前分支的代码,拿到最新的代码,git pull是获取已经存在的仓库 ...

  9. html5 css选择器。 井号,句号的区别

    .理解CSS的样式组成CSS里的样式表是有规则组成的,每条规则有三个部分组成:1.选择器(如下面例子中的:“body”),告诉浏览器文档的哪个部分受规则影响:2.属性(如实例中的font-family ...

  10. PopupWindow 以及拍照、裁剪

    实现这样的效果 圆角图片的自定义控件直接拷进来,和com一个等级 想要弹出内容可以使用悬浮窗 layout_pupup <LinearLayout xmlns:android="htt ...