leetcode — sort-colors
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的更多相关文章
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- [LeetCode] Sort Colors 颜色排序
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- [leetcode]Sort Colors @ Python
原题地址:https://oj.leetcode.com/problems/sort-colors/ 题意: Given an array with n objects colored red, wh ...
- [Leetcode] Sort Colors (C++)
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 75.[LeetCode] Sort Colors
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- [LeetCode] Sort Colors 对于元素取值有限的数组,只遍历一遍的排序方法
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- [LeetCode] Sort Colors 只有3个类型的排序
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- LeetCode Sort Colors (技巧)
题意: 一个数组只可能含有3种数据,分别为1,2,3,请将数组排序(只能扫一遍). 思路: 如果扫两遍的话,用个桶记录一下,再赋值上去就行了. class Solution { public: voi ...
- 【LeetCode】Sort Colors 数组排序
题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...
- 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 ...
随机推荐
- day 34
1 .内容回顾 #__author : 'liuyang' #date : 2019/4/17 0017 上午 9:01 # 利大于弊,则做之 # 会死于斯,则不去 # 2个 人 晚上 5个题 面试题 ...
- 前后台分离开发时遇到循环引用问题"$ref"
1. 遇到的问题 { "errMsg": "", "data": { "baseinfo": { "freeT ...
- Linux下安装numpy
转自:https://blog.csdn.net/abc_321a/article/details/82056019 1.下载源码包 ,命令如下 wget http://jaist.dl.source ...
- redis_简单动态字符串
在redis中,C字符串(以'\0'结尾的字符数组)只用在一些无需对字符串值进行修改的地方,比如打印日志.其他情况,redis使用SDS - SimpleDynamicString 简单动态字符串,来 ...
- .Net QQ互联教程
qq互联只需要备案即可申请,申请成功后可以到qq互联官网查看教程,本站开始想使用js的教程但是由于本站需要绑定本站的账号用js教程无法完成,所以使用原始的oauth2.0来完成. 申请qq互联接口 q ...
- python_flask框架学习之路(1)
1.初识web,了解utl . 术语: scheme://host:port/path?query-string=xxx#yyyy 例子:https://i.cnblogs.com/EditArtic ...
- jsp获取当前项目跟路径
在jsp中获取当前项目的根路径: <% String basePath = request.getScheme() + "://"+ request.getServerNam ...
- .Net Trace->Listeners->Remove
今天在调试一个别人写的ASP.NET老程序,log文件怎么都写不了.web.config里的trace->listeners里有这么一行: <remove type="Syste ...
- python基础学习记录一
1.如果脚本中带有中文(中文注释或者中文字符串,中文字符串前面需要在前面加u),且需要在文件头注明编码,并将UTF-8编码格式 #-*-coding:utf-8 -*- printf u'你好,WOR ...
- 使用Kubeadm搭建Kubernetes(1.12.2)集群
Kubeadm是Kubernetes官方提供的用于快速安装Kubernetes集群的工具,伴随Kubernetes每个版本的发布都会同步更新,在2018年将进入GA状态,说明离生产环境中使用的距离越来 ...