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的更多相关文章

  1. leetcode — sort-colors

    import java.util.Arrays; /** * Source : https://oj.leetcode.com/problems/sort-colors/ * * * Given an ...

  2. sort-colors——排序3种数字

    题目描述 Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  3. Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  4. [LeetCode] Sort Colors 颜色排序

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  5. Leetcode分类刷题答案&心得

    Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...

  6. Leetcode 75. Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  7. leetcode算法分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  8. 全部leetcode题目解答(不含带锁)

    (记忆线:当时一刷完是1-205. 二刷88道.下次更新记得标记不能bug-free的原因.)   88-------------Perfect Squares(完美平方数.给一个整数,求出用平方数来 ...

  9. [leetcode] 题型整理之排序

    75. Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects ...

随机推荐

  1. 第六篇 Flask中的路由系统

    Flask中的路由系统其实我们并不陌生了,从一开始到现在都一直在应用 @app.route("/",methods=["GET","POST" ...

  2. oracle 特殊符号替换删除处理

    1 获取ascii码 select ascii('特殊字符') from dual 2 替换 update table set testfield= replace(testfield,chr(asc ...

  3. [2018-08-03] python开发个人资源共享网--第一天

    项目需求-环境搭建 python版本:python 3.6.2 开发工具:PyCharm 数据库:MySql5.7.24 数据库管理工具:Navicat 环境搭建完毕 ---------------- ...

  4. Kong01-Kong 介绍

    概述 Kong 是在客户端和(微)服务间转发API通信的API网关,通过插件扩展功能. Kong 的官方网站,https://konghq.com/kong Kong 的官方 Github 站点:ht ...

  5. 无聊的 邮递员 插头dp

    邮递员想知道,如果他每天都用不同路线走过10×20个点阵邮筒,他必须活过多少个世纪才能走遍所有方案? 7:00 改完T1,开始肝插头dp 7:10 放弃,颓博客 7:20 学习插头dp 7:21 放弃 ...

  6. 史上最全的excel读写技术分享

    目录 简介 导出excel常用的几种方法 POI CSV jxl jxls easyexcel 快速入门 代码解读 总结 常用API 单元格样式 合并单元格 数据样式 多sheet设置 单元格添加超链 ...

  7. Windows Server 搭建企业无线认证(Radius认证方案)

    认证协议介绍: 扩展认证协议EAP(Extensible Authentication Protocol) 是一个在无线网络或点对点连线中普遍使用的认证框架.它被定义在RFC 3748中,并且使RFC ...

  8. 前端与算法 leetcode 48. 旋转图像

    目录 # 前端与算法 leetcode 48. 旋转图像 题目描述 概要 提示 解析 解法一:转置加翻转 解法二:在单次循环中旋转 4 个矩形 算法 传入测试用例的运行结果 执行结果 GitHub仓库 ...

  9. Webpack 4 Tree Shaking 终极优化指南

    几个月前,我的任务是将我们组的 Vue.js 项目构建配置升级到 Webpack 4.我们的主要目标之一是利用 tree-shaking 的优势,即 Webpack 去掉了实际上并没有使用的代码来减少 ...

  10. Appium+python自动化(四十)-Appium自动化测试框架综合实践 - 代码实现(超详解)

    1.简介 今天我们紧接着上一篇继续分享Appium自动化测试框架综合实践 - 代码实现.由于时间的关系,宏哥这里用代码给小伙伴演示两个模块:注册和登录. 2.业务模块封装 因为现在各种APP的层出不群 ...