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 ...
随机推荐
- jQuery获取元素的方法
1·$(".box").offset().left 获取盒子左边到浏览器左侧的距离(上右下相同): 2·$("span").width() 获取盒子的宽度(高度 ...
- IDEA的Find菜单使用
想要显示Find菜单在底部,如图: 选中一个类,如Cloneable,然后按键Ctrl+Alt+B(相当于eclipse中的ctrl+T) 显示如图,选择最右上角的固定标签 就会把它固定在控制台菜单中 ...
- LinkeList 特有方法
LinkedList:特有方法:addFirst();addLast();添加元素到集合,添加到头尾,getFirst();getLast();获取元素,但不删除元素.如果集合中没有元素,会出现NoS ...
- Maths | 病态问题和条件数
目录 1. 概念定义 1.1. 病态/ 良态问题 1.2. 适定/ 非适定问题 1.3. 良态/ 病态矩阵和条件数 2. 病态的根源 3. 计算条件数的方法 3.1. 与特征值的关系 3.2. 与奇异 ...
- Javascript Engine, Java VM, Python interpreter, PyPy – a glance
提要: url anchor (ajax) => javascript engine (1~4 articles) => java VM vs. python interpreter =& ...
- azure cosmos db (mongo DB)
使用.net mongo的操作类操作azure(微软云)cosmosdb时,发现在做delete的操作的时候可以传一个文档对象,但是最后这个文档会解析成具体的sql语句,而当文档特别大时这样就出先了转 ...
- PYTHON进阶(3)
学习内容: 1.Python模块redis 2.Python模块memcach 3.Python模块SQLAlchemy 一.Python模块redis redis介绍 二.Python模块memca ...
- 1405ST软件测试课的要求补充说明
期末成绩由考勤.测试小结报告.实验报告.课堂问答.测试总结报告.(暂不安排笔试) 分数分配:考勤10%,测试小结(双周结)报告10%,实验报告30%,课堂问答20%,测试报告30% 考勤:任选10次 ...
- CLion之C++框架篇-安装工具,基础框架的搭建(一)
背景 日常学习C++,也就是看看书.在vim里写写代码.在日常项目开发中,也是边看书(一是系统性理解.二是找找有什么更好的代码编写方式)边写代码,会顺带看看别人的代码怎么写的? 日常学 ...
- html input file accept 上传文件类型限制格式 MIME 类型列表
例: <input type="file" accept="application/vnd.openxmlformats-officedocument.spread ...