Sort Colors leetcode java
题目:
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?
题解:
这道题就是运用指针来解决了,可以说叫3指针吧。
一个指针notred从左开始找,指向第一个不是0(红色)的位置;一个指针notblue从右开始往左找,指向第一个不是2(蓝色)的位置。
然后另一个新的指针i指向notred指向的位置,往后遍历,遍历到notred的位置。
这途中需要判断:
当i指向的位置等于0的时候,说明是红色,把他交换到notred指向的位置,然后notred++,i++。
当i指向的位置等于2的时候,说明是蓝色,把他交换到notblue指向的位置,然后notred--。
当i指向的位置等于1的时候,说明是白色,不需要交换,i++即可。
代码如下:
1 public static void swap(int A[], int i, int j){
2 int tmp = A[i];
3 A[i]=A[j];
4 A[j]=tmp;
5 }
6
7 public static void sortColors(int A[]) {
8 if(A == null || A.length==0)
9 return;
int notred=0;
int notblue=A.length-1;
while (notred<A.length&&A[notred]==0)
notred++;
while (notblue>=0&&A[notblue]==2)
notblue--;
int i=notred;
while (i<=notblue){
if (A[i]==0) {
swap(A,i,notred);
notred++;
i++;
}else if (A[i]==2) {
swap(A,i,notblue);
notblue--;
}else
i++;
}
}
Sort Colors leetcode java的更多相关文章
- 75. Sort Colors - LeetCode
Question 75. Sort Colors Solution 题目大意: 给一个数组排序,这个数组只有0,1,2三个元素,要求只遍历一遍 思路: 记两个索引,lowIdx初始值为0,highId ...
- Sort Colors [LeetCode]
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- Sort Colors —— LeetCode
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- Sort List leetcode java
题目: Sort a linked list in O(n log n) time using constant space complexity. 题解: 考虑到要求用O(nlogn)的时间复杂度和 ...
- Insertion Sort List Leetcode java
题目: Sort a linked list using insertion sort. 题解: Insertion Sort就是把一个一个元素往已排好序的list中插入的过程. 初始时,sorted ...
- 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 ...
- LeetCode 75. 颜色分类(Sort Colors) 30
75. 颜色分类 75. Sort Colors 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中, ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- [Leetcode Week2]Sort Colors
Sort Colors题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/sort-colors/description/ Description Give ...
随机推荐
- 回文树练习 Part1
URAL - 1960 Palindromes and Super Abilities 回文树水题,每次插入时统计数量即可. #include<bits/stdc++.h> using ...
- Linux教程 - 管道和重定向
管道和重定向! 保持数据流动 介绍 在前两节中,我们看了一些可以为我们操作数据的过滤器.在本节中,我们将看到我们如何将它们结合在一起来执行更强大的数据操作. 本节涉及一些阅读.即使这些机制及其 ...
- git merge和git rebase的区别(转)
Description git rebase 和 git merge 一样都是用于从一个分支获取并且合并到当前分支,但是他们采取不同的工作方式,以下面的一个工作场景说明其区别 场景: 如图所示: ...
- BZOJ 2115: [Wc2011] Xor 线性基 dfs
https://www.lydsy.com/JudgeOnline/problem.php?id=2115 每一条从1到n的道路都可以表示为一条从1到n的道路异或若干个环的异或值. 那么把全部的环丢到 ...
- 异常:The server committed a protocol violation
异常记录: Exception rethrown at [0]: 在 Wintop.Windows.FrmLogin.btnLogin_Click(Object sender, EventArgs e ...
- bzoj 1027 floyd求有向图最小环
结合得好巧妙.... 化简后的问题是: 给你两个点集A,B,求B的一个子集BB,使得BB的凸包包含A的凸包,求BB的最小大小. 先特判答案为1,2的情况,答案为3的情况,我们先构造一个有向图: 对于B ...
- LogStash日志分析系统
简介 通常日志管理是逐渐崩溃的——当日志对于人们最重要的时候,也就是出现问题的时候,这个渐进的过程就开始了.日志管理一般会经历一下3个阶段: 初级管理员将通过一些传统工具(如cat.tail.sed. ...
- Spring_Task初探(注解,XML配置)
这几天想写一个动态添加任务项目找了找Spring下的自带定时功能发现还真有,然后网上找了找资料写了个demo 写了两种方式来执行定时的任务(XML配置和注解) 先建两个普通的任务类(XML配置调用的任 ...
- systemtap 探针定制
http://blog.163.com/digoal@126/blog/static/163877040201391123645546/
- node.js第11课(HTTPclient)
一.http模块提供了两个函数http.request和http.get,功能是作为client向HTTPserver发起请求. Ext.Ajax.request({},function ...