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. uniapp cli版本中如何引入scss?

    一.安装依赖 npm i node-sass@4.14.1 sass-loader -D 二.在脚手架版本新建项目成功后,官方为我们准备了uni.scss文件,在这个里面写即可全局使用. ... 一. ...

  2. Alpha冲刺-第四次冲刺笔记

    Alpha冲刺-冲刺笔记 这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzzcxy/2018SE2 这个作业要求在哪里 https://edu.cnblogs. ...

  3. vue绑定用户页面

    1.vue微博回调空页面 微博回调空页面为: http://127.0.0.1:8888/oauth/callback/ 1.1 页面路径 components\oauth.vue <templ ...

  4. PyQt(Python+Qt)学习随笔:部件的inputMethodHints属性

    inputMethodHints属性只对输入部件有效,输入法使用它来检索有关输入法应如何操作的提示,例如,如果设置了只允许输入数字的标志,则输入法可能会更改其可视组件,以反映只能输入数字.相关取值及含 ...

  5. 关于RequestParam在不同的Spring版本上,接口在controller重载时注解可能失效的踩坑记录

    先抛背景: 我项目中的Spring版本是2.0.3.RELEASE. api-demo负责暴露接口,service-demo负责实现功能.接口参数的@RequestParam和@RequestBody ...

  6. [BJDCTF2020]Cookie is so stable && [GWCTF 2019]枯燥的抽奖

    [BJDCTF2020]Cookie is so stable 进入环境后看到有hint,点击之后查看源代码 提示我们cookie有线索 flag页面是: 需要输入一个username,或许这道题目是 ...

  7. C# 继承类的值赋

    C# 继承类的值赋 /// <summary> /// 将父类的值赋值到子类中 /// </summary> /// <typeparam name="TPar ...

  8. 威联通(NAS)搭建个人音乐中心

    我为什么要自己搭建音乐服务 曾记得早些年,音乐是可以随便在线听,随便下载的,没有付费这么一说的(背后是音乐平台提供的版权支持).我们听音乐也就可以很随意,但是这几年,音乐的版权开始管理的严禁,音乐没地 ...

  9. Panda交易所视点观察:政府连发区块链建设文件,相关概念股受追捧

    日前,Panda交易所从北京市地方金融监督管理局获悉,证监会已同意在北京.苏州.上海.浙江.深圳等地区的区域性股权市场参与区块链建设工作.以上5市金融监管局将按照中国证监会的统一部署要求推进建设工作. ...

  10. 【学习笔记】动态 dp 入门简易教程

    序列 dp 引入:最大子段和 给定一个数列 \(a_1, a_2, \cdots, a_n\)(可能为负),求 \(\max\limits_{1\le l\le r\le n}\left\{\sum_ ...