sortColors
Given an array with n objects colored red, white or blue, sort them in-place 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.
Example:
Input: [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/sort-colors
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解法1:遍历元素,把0调到数组的前面,把2调到数组后面,1不用处理:
public void sortColors(int[] nums) {
int length = nums.length;
int temp,index;
index=0;
for(int i = 0; i < length; i++)
{
if(nums[i] == 0)
{
temp = nums[index];
nums[index++] = nums[i];
nums[i] = temp;
}
}
index=length - 1;
//这里其实可以加个限定条件,因为是从数组末端开始遍历的,当遍历到0的时候,就已经不用再往前遍历了,因此也可节省一丁点时间,不过好像问题并不大,需要优化的是内存方面。。。
for(int j = length - 1; j >= 0; j--)
{
if(nums[j] == 2)
{
temp = nums[index];
nums[index--] = nums[j];
nums[j] = temp;
}
}
}
解法2:遍历计数0,1,2的个数,再把原数组重新赋值:
public void sortColors(int[] nums) {
int length = nums.length;
int count = 0;
int count1 = 0;
int count2;
for(int i = 0; i < length; i++)
{
if(nums[i] == 0)
{
nums[count] = 0;
count++;
}
else if(nums[i] == 1)
{
count1++;
}
}
count2 = length - (count + count1);
for(int i = count; i < count + count1; i++)
{
nums[i] = 1;
}
for(int i = count+count1; i < length; i++)
{
nums[i] = 2;
}
}
sortColors的更多相关文章
- leetcode — sort-colors
import java.util.Arrays; /** * Source : https://oj.leetcode.com/problems/sort-colors/ * * * Given an ...
- sort-colors——排序3种数字
题目描述 Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 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 颜色排序
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- Leetcode分类刷题答案&心得
Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...
- 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算法分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- 全部leetcode题目解答(不含带锁)
(记忆线:当时一刷完是1-205. 二刷88道.下次更新记得标记不能bug-free的原因.) 88-------------Perfect Squares(完美平方数.给一个整数,求出用平方数来 ...
- [leetcode] 题型整理之排序
75. Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects ...
随机推荐
- Python装饰器基础
一.Python装饰器引入 讲 Python 装饰器前,我想先举个例子,虽有点污,但跟装饰器这个话题很贴切. 每个人都有的内裤主要功能是用来遮羞,但是到了冬天它没法为我们防风御寒,咋办?我们想到的一个 ...
- 学习笔记63_python反射
####反射预备知识一########### __call__ 对象后面加括号,触发执行. python中,类的默认的内置方法,有一个名为__call__,如 class foo: def __in ...
- [考试反思]1011csp-s模拟测试69:无常
承蒙大脸skyh的毒奶,加之以被kx和Parisb以及板儿逼剥夺了一中午的睡眠(其实还有半个晚上)RP守恒终于失效了,连续两场没考好 RP也是不够了,竟然考原题,而且还不换题,连样例都一模一样只不过加 ...
- Head First设计模式——单例模式
单例模式是所有设计模式中最简单的模式,也是我们平常经常用到的,单例模式通常被我们应用于线程池.缓存操作.队列操作等等. 单例模式旨在创建一个类的实例,创建一个类的实例我们用全局静态变量或者约定也能办到 ...
- 五角场之殇。曾与张江、漕河泾、紫竹齐名。如今,上海四大IT科技园是否还在?
五角场.张江.漕河泾.紫竹并称为上海四大 IT 科技园.张江与漕河泾有着最多的国内互联网公司,以及部分的外企.随着国内互联网公司的崛起,张江与漕河泾名声远扬,不仅在上海IT圈人尽皆知,在全国范围也是小 ...
- 测试工程师,选择python还是java?
问:“你平时工作中,用java多还是用python多”? 答:“都还可以,根据具体的场景选择不同的语言”. 问:“比如说呢”? 答:“开发自己的测试平台,肯定会选择java:在centos服务器跑一些 ...
- 在linux上使用ssh登录服务器,Linux权限
本文是作者原创,版权归作者所有.若要转载,请注明出处 ssh为Secure Shell(安全外壳协议)的缩写. 很多ftp.pop和telnet在本质上都是不安全的. 我们使用的Xshell6就是基于 ...
- [转载]2.6 UiPath循环嵌套的介绍和使用
一.循环嵌套的介绍 一个循环体内又包含另一个完整的循环结构,就称之为循环嵌套.内嵌的循环中还可以嵌套循环,这就是多层循环,也叫做多重循环. 二.在UiPath中结合使用循环嵌套生成99乘法表 1.打开 ...
- python中程序的异常处理
什么叫异常? 导致程序异常退出叫做异常 try...except...else 如果要抓取某种特定异常可以用except ERROR as e else:如果程序正常执行那么会执行else里面的代码 ...
- 使用Samba服务实现文件共享
1.在虚拟机上安装Samba服务安装包 (在下载之前检查客户机与服务器是否能够ping通) (鼠标右击桌面,打开终端,测试和yum是否能够ping通,下面的命令行是我的yum的IP地址) [root@ ...