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. Newtonsoft.Json 时间格式化

    时间序列化经常多个T:“2017-01-23T00:00:00” 解决方案: 日期格式化输出,指定IsoDateTimeConverter的DateTimeFormat即可 IsoDateTimeCo ...

  2. B树/[oracle]connect BY语句

    读大神的书,出现很多没有见过的函数和便捷操作,特此记录 connect by 之前没有接触过,为了学习这个语句,先了解一下B树数据类型是最好的方法. [本人摘自以下博客] https://www.cn ...

  3. 用JavaScript制作简单的计算器

    <html > <head> <title>简单计算器</title> <style type="text/css"> ...

  4. 28.TreeSet

    与HashSet是基于HashMap实现一样,TreeSet同样是基于TreeMap实现的.在前一篇中详细讲解了TreeMap实现机制,如果客官详细看了这篇博文或者对TreeMap有比较详细的了解,那 ...

  5. Servlet发送邮件遇到的问题SMTPSendFailedException 554

    接到通知,一个接收用户请求的邮箱有段时间收不到邮件了.当时想着这么简单的功能,就没有加上日志记录.重写程序后,日志记下的报错是:SMTP的SMTPSendFailedException 554  co ...

  6. git了解-使用笔记

    1.Git的由来与设计理念 Git是linux之父Linus Torvalds开发的,是一款最先进的项目版本控制系统. Git的由来有一个小故事,传闻起初Linux社区工作者都是通过邮件的,发送给li ...

  7. 自动化单元测试工具 EvoSuite 的简单使用 【转载】

    转载:https://www.cnblogs.com/hughding/p/evosuite.html 一.EvoSuite简介 EvoSuite是由Sheffield等大学联合开发的一种开源工具,用 ...

  8. eclipse的这几个小玩意

    scroll lock   滚动锁定 word wrap  自动换行 show console  when standard  out changes   标准输出更改时显示控制台 show cons ...

  9. 如果这样来理解HTTPS,一篇就够了!

    1.前言 可能有初学者会问,即时通讯应用的通信安全,不就是对Socket长连接进行SSL/TLS加密这些知识吗,干吗要理解HTTPS协议呢. 这其实是个误解:当今主流的移动端IM数据通信,总结下来无外 ...

  10. Android Studio 3.0 变化之 implementation与compile

    Android Studio 3.0 出来很久了,本文就着重介绍一下 新版本中 Moudle 中 build.gradle 文件中的变化. 我们来看看新建一个项目在 Moudle 中的 depende ...