[leetcode]75.Sort Color三指针
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三指针的更多相关文章
- [leetcode]75. Sort Colors三色排序
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- LeetCode 75. Sort Colors (颜色分类):三路快排
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- [LeetCode] 75. Sort Colors 颜色排序
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- LeetCode 75. Sort Colors(排序颜色)
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- Leetcode 75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- LeetCode 75 Sort Colors(颜色排序)
翻译 给定一个包括红色.白色.蓝色这三个颜色对象的数组.对它们进行排序以使同样的颜色变成相邻的,其顺序是红色.白色.蓝色. 在这里,我们将使用数字0.1和2分别来代表红色.白色和蓝色. 原文 Give ...
- leetCode 75.Sort Colors (颜色排序) 解题思路和方法
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- leetcode 75. Sort Colors (荷兰三色旗问题)
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- leetcode 75 Sort Colors 计数排序,三路快排
解法一:计数排序:统计0,1,2 的个数 时间复杂度:O(n) 空间复杂度:O(k) k为元素的取值范围, 此题为O(1) class Solution { public: void sortC ...
随机推荐
- rest-framework 解析器
一 解析器的作用: 根据请求头 content-type 选择对应的解析器对请求体内容进行处理. 有application/json,x-www-form-urlencoded,form-data等格 ...
- SSM之Spring框架--->>墨盒案例
分析: 程序中包括打印机(Printer).墨盒(Ink).和纸张(Paper).三类组件 首先创建一个新项目文件Spring_Box 我们来定义墨盒和纸张的接口类 墨盒接口Ink.java文件内容如 ...
- 解决IDEA更新为最新的2020.3版后,右键运行居然没有以xml形式运行的Run显示
一.前言 个人一直喜欢用IDEA最新版,结果更新后,发现TestNg批量执行,选中testng.xml右键没Run,如下图: 刚开始以为是配置错误呢,下载了2018.2版本的IDEA,还能正常运行,于 ...
- 阿里云服务器搭建Docker版AWVS
本文严重参考该文章:https://www.sqlsec.com/2020/04/awvs.html 阿里云服务器搭建Docker版AWVS,因为之前有使用Docker的经验,所以本文只是简述一下安装 ...
- CTF流量分析题大全(掘安攻防平台)
突然想做一下流量分析题,记得掘安攻防实验室上面有很多的流量分析题目,故做之 流量分析题一般使用的都是wireshark,(流量分析工具中的王牌 夺取阿富汗 说了分析http头,所以直接过滤http协议 ...
- element ui的el-radio踩坑
1.html 1 <div class="listPeopleDetail"> 2 <div class="item" v-for=" ...
- Markdown常用数学符号&公式
符号 代码 描述 \(\sim\) $\sim$ 波浪号 \(\sum\) $\sum$ 求和公式 \(\sum_{i=0}^n\) $\sum_{i=0}^n$ 求和上下标 \(\times\) $ ...
- Java并发编程的艺术(八)——锁相关
锁的作用 控制多个线程访问共享资源. 线程协作 Lock接口 特点 与synchronized类似的同步功能,只是需要显式地获取和释放锁.缺少隐式获取锁的便捷性. 拥有锁获取与释放的可操作性.可中断的 ...
- linux下安装Zookeeper 3.4.14
1.下载Zookeeper 3.4.14(https://zookeeper.apache.org/) wget https://mirror.bit.edu.cn/apache/zookeeper/ ...
- Struts2 S2-061漏洞复现(CVE-2020-17530)
0x01 漏洞描述 Struts2 会对某些标签属性(比如 `id`,其他属性有待寻找) 的属性值进行二次表达式解析,因此当这些标签属性中使用了 `%{x}` 且 `x` 的值用户可控时,用户再传入一 ...