import java.util.Arrays;

/**
* 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. click to show follow up. 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?
*/
/*
* 两种解法
* 1.三个指针:红指针,蓝指针,遍历指针(循环指针i),红蓝指针比别从前后端记录红色区域和蓝色区域的边界位置,
* 遍历指针负责找到红蓝颜色的数据,白色的不用管,最后中间剩下的就是白色
* 2.记录红白蓝三色出现的次数,最后直接对数组进行相应的赋值
* 这里选用第一种
* 容易出错的地方:遍历指针和边界指针交换之后,还要在遍历指针的位置再判断一下是不是其他颜色,
* 所以比较适合写成两个if分别判断,而且后边的if要加i--*/
public class Q75SortColors {
public static void main(String[] args) {
int[] nums = new int[]{2,2,2};
sortColors(nums);
System.out.println(Arrays.toString(nums));
}
public static void sortColors(int[] nums) {
int red = 0;
int blue = nums.length - 1;
int temp;
//获取红色区域的初始边界
for (int i =0;i < nums.length;i++)
{
if (nums[i] != 0)
{
red = i;
break;
} }
//获取蓝色区域的初始边界
for (int i =nums.length-1;i >= 0 ;i--)
{
if (nums[i] != 2)
{
blue = i;
break;
} }
//遍历交换归位
for (int i =red;i <= blue ;i++)
{
if (nums[i] == 0)
{
temp = nums[red];
nums[red] = nums[i];
nums[i] = temp;
red++;
}
if (nums[i] == 2)
{
temp = nums[blue];
nums[blue] = nums[i];
nums[i] = temp;
blue--;
i--;
}
}
}
}

[leetcode]75.Sort Color三指针的更多相关文章

  1. [leetcode]75. Sort Colors三色排序

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  2. LeetCode 75. Sort Colors (颜色分类):三路快排

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  3. [LeetCode] 75. Sort Colors 颜色排序

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  4. LeetCode 75. Sort Colors(排序颜色)

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  5. Leetcode 75. Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  6. LeetCode 75 Sort Colors(颜色排序)

    翻译 给定一个包括红色.白色.蓝色这三个颜色对象的数组.对它们进行排序以使同样的颜色变成相邻的,其顺序是红色.白色.蓝色. 在这里,我们将使用数字0.1和2分别来代表红色.白色和蓝色. 原文 Give ...

  7. leetCode 75.Sort Colors (颜色排序) 解题思路和方法

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  8. leetcode 75. Sort Colors (荷兰三色旗问题)

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  9. leetcode 75 Sort Colors 计数排序,三路快排

    解法一:计数排序:统计0,1,2 的个数 时间复杂度:O(n) 空间复杂度:O(k)    k为元素的取值范围, 此题为O(1) class Solution { public: void sortC ...

随机推荐

  1. devc++编译时 undefined reference to `__imp_WSAStartup'

    socket编程时遇到的问题:

  2. Beta冲刺随笔——Day_Two

    这个作业属于哪个课程 软件工程 (福州大学至诚学院 - 计算机工程系) 这个作业要求在哪里 Beta 冲刺 这个作业的目标 团队进行Beta冲刺 作业正文 正文 其他参考文献 无 今日事今日毕 林涛: ...

  3. 【GDKOI2014】JZOJ2020年8月13日提高组T4 内存分配

    [GDKOI2014]JZOJ2020年8月13日提高组T4 内存分配 题目 Description Input Output 输出m行,每行一个整数,代表输入中每次程序变化后系统所需要的空闲内存单位 ...

  4. CPU实现原子操作的原理

    586之前的CPU, 会通过LOCK锁总线的形式来实现原子操作. 686开始则提供了存储一致性(Cache coherence),  这是多处理的基础, 也是原子操作的基础. 1. 存储的粒度 存储的 ...

  5. 自动化运维工具之Puppet模块

    前文我们了解来puppet的变量.流程控制.正则表达式.类和模板的相关话题,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/14079208.html:今天我们来 ...

  6. Linux 硬盘挂载及开机挂载

    一.分区 主分区.扩展分区.逻辑分区的区别 主分区:包含操作系统启动所必需的文件和数据的硬盘分区,如需在硬盘上安装操作系统,该硬盘必须得有一个主分区 扩展分区:除主分区外的分区,不能直接使用,必须再划 ...

  7. PyQt(Python+Qt)学习随笔:Qt Designer中部件的 baseSize和sizeIncrement

    1.baseSize 部件的 baseSize是部件的基础大小(单位:像素),如果部件设定了sizeIncrement,该属性用于在调整部件尺寸时计算部件应该调整到的合适值,但这个属性缺省值是QSiz ...

  8. LeetCode初级算法之字符串:344 反转字符串

    反转字符串 题目地址:https://leetcode-cn.com/problems/reverse-string/ 编写一个函数,其作用是将输入的字符串反转过来.输入字符串以字符数组 char[] ...

  9. 小齐读者拿到快手、百度、网易等 offer 的独门秘籍!

    小齐说: 这篇文章来自读者冰红茶,他刚结束了秋招,拿了很多家 offer. 和他聊完之后,我觉得他的备考思路也完全适用于美国的面试,只是分值要调整一下,但是具体每一块的内容,地球村通用.所以分享给大家 ...

  10. Day2 Scrum 冲刺博客

    线上会议: 昨天已完成的工作与今天计划完成的工作及工作中遇到的困难: 成员姓名 昨天完成工作 今天计划完成的工作 工作中遇到的困难 纪昂学 总结会议内容,思考自己所分配到的任务 创建一个Cell类,用 ...