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 ...
随机推荐
- HyperLPR车牌识别
简介 本文基于HyperLPR进行修改,完整代码参考https://github.com/Liuyubao/PlateRecognition. HyperLPR是一个使用深度学习针对对中文车牌识别的实 ...
- Python的深浅拷贝
Python的深浅拷贝 深浅拷贝 1. 赋值,对于list, set, dict来说, 直接赋值. 其实是把内存地址交给变量并不是复制一份内容 list1 = [']] list2 = list1 p ...
- C++等号操作符重载
在新学操作符重载时最令人头疼的可能就是一些堆溢出的问题了,不过呢,只要一步步的写好new 与 delete.绝对不会有类似的问题. 当时我们编译可以通过,但是运行会出错,因为对象s1与s2进行赋值时, ...
- python线程threading.Timer源码解读
threading.Timer的作用 官方给的定义是: """Call a function after a specified number of seconds: t ...
- cnblogs侧边栏访客统计 小插件
之前博客的侧边栏一直用的是flagcounter,直观简洁又好看,近期恍然发现被博客园禁了.禁用原因据说是由于flagcounter将香港(HongKong).台湾(TaiWan)和澳门(Macau) ...
- MyBatis --- 映射关系【一对一、一对多、多对多】,懒加载机制
映射(多.一)对一的关联关系 1)若只想得到关联对象的id属性,不用关联数据表 2)若希望得到关联对象的其他属性,要关联其数据表 举例: 员工与部门的映射关系为:多对一 1.创建表 员工表 确定其外键 ...
- JenKins结合cppcheck及cpplint进行代码风格及静态代码检测
JenKins结合cppcheck及cpplint 最近公司需要在Jenkins上安装cppcheck及cpplint进行代码风格及静态代码检测,这里记录下过程. 前提条件 安装了Jenkins 步骤 ...
- Java开发者入职必备条件
01.基础技术体系 我认为知识技能体系化是判断技术是否过关的第一步.知识体系化包含两层含义: 1. 能够知道技术知识图谱(高清版图谱扫文末二维码)的内容 比如分布式系统中常用的RPC技术,其背后就涉及 ...
- CSS中越界问题的经典解决方案
(1)如何解决父元素的第一个子元素的margin-top越界问题 1)为父元素加border-top: 1px;——有副作用 2)为父元素指定padding-top: 1px;——有副作用 3)为父元 ...
- ReactJS中的自定义组件
可控自定义组件: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...