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 ...
随机推荐
- 2018.8.1 python中字典的增删改查及其它操作
一.字典的简单介绍 1.dict 用{}来表示 键值对数据 {key:value} 唯一性 2.键都必须是可哈希,不可变的数据类型就可以当做字典中的键 值没有任何限制 ...
- spring @Value("${name}")使用
在springmvc.xml配置文件中加入 (注意是springmvc配置文件不是spring配置文件的xml)不然可能取不到值 <context:property-placeholder lo ...
- Android Studio Module 引入aar
1.把aar文件放到module的libs目录下 2.作为lib的module的gradle文件: repositories { flatDir { dirs 'libs' } } dependenc ...
- Android原生PDF功能实现
1.背景 近期,公司希望实现安卓原生端的PDF功能,要求:高效.实用. 经过两天的调研.编码,实现了一个简单Demo,如上图所示. 关于安卓原生端的PDF功能实现,技术点还是很多的,为了咱们安卓开发的 ...
- Docker应用部署
MySQL: #拉取mysql镜像 docker pull centos/mysql--centos7 #创建容器 #-p 端口映射 -e添加环境变量MYSQL_ROOT_PASSWORD 是root ...
- MIT线性代数:4.A的LU分解
- 网络安全-主动信息收集篇第二章SNMP扫描
SNMP扫描: snmp在中大型企业中可以用来做网络管理和网络监控的使用,当开启了snmp简单网络管理后,那么客户机就可以通过这个协议向该设备发送snmp协议内容可以轻松查询到目标主机的相关信息. 以 ...
- [BZOJ] DZY Loves Math 系列 I && II
为了让自己看起来有点事干 ,做个套题吧..不然老是东翻翻西翻翻也不知道在干嘛... \(\bf 3309: DZY \ Loves \ Math\) 令 \(h=f*\mu\) 很明显题目要求的就是\ ...
- Vue躬行记(6)——内容分发
Vue提供了一种内容分发技术,可将父组件中的内容传递给子组件的模板,实现方式参照了Web组件规范草案. 一.插槽 Vue内置了一个<slot>元素,能作为插槽(slot)存在,而插槽内可包 ...
- 使用Typescript重构axios(一)——写在最前面
0.系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三)- ...