Leetcode_75_Sort Colors
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43302343
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.
思路:
(1)题意为给定一个数组,里面含有红白蓝三种颜色(顺序是打乱的),将其调整为红-->白-->蓝的顺序。其中,0,1,2分别对应红,白,蓝。
(2)该题的实质就是一个排序操作。只不过相同的元素比较多,需要确定下来。本文使用的方法比较简单,首先,遍历数组,确定0,1,2的个数;然后,再对数组进行遍历,依次将0,1,2填入数组中。具体操作为,只要0的个数大于零,就将0填入数组,然后个数减1,直到0全部放入数组为止,继续将1和2按照其对应的个数放入数组中。最后,所得的数组中就是以连续的0-->1-->2的顺序分布。
(3)希望本文对你有所帮助。
算法代码实现如下:
/** * * @author liqq */ public void sortColors(int[] A) { if (A == null || A.length <= 1) return; int _zeor = 0; int _one = 0; int _two = 0; for (int i = 0; i < A.length; i++) { if (A[i] == 0) _zeor++; if (A[i] == 1) _one++; if (A[i] == 2) _two++; } for (int i = 0; i < A.length; i++) { if (_zeor > 0) { A[i] = 0; _zeor--; continue; } if (_one > 0) { A[i] = 1; _one--; continue; } if (_two > 0) { A[i] = 2; _two--; } } }
Leetcode_75_Sort Colors的更多相关文章
- 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 75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- CF444C. DZY Loves Colors[线段树 区间]
C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces444C DZY Loves Colors(线段树)
题目 Source http://codeforces.com/problemset/problem/444/C Description DZY loves colors, and he enjoys ...
- Sort Colors [LeetCode]
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 【LeetCode】Sort Colors
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- PAT (Advanced Level) Practise:1027. Colors in Mars
[题目链接] People in Mars represent the colors in their computers in a similar way as the Earth people. ...
- LintCode Sort Colors
For this problem we need to sort the array into three parts namely with three numbers standing for t ...
随机推荐
- ngx.re.match使用示例
s='...12ab345cde...' r, e = ngx.re.match(s,'(\\d+)([a-z]+)(?<num>\\d+)(?<word>[a-z]+)') ...
- django+uwsgi+nginx+postgresql备忘
安装pg创建数据库xxx设置用户密码111111 apt-get install postgresql su - postgres psql create database xxx; alter us ...
- Objective-C数据结构
Objective-C数据结构 枚举 typedef enum { SexMan, SexWoman } Sex; 结构体 typedef struct { int year; int month; ...
- Scala:数组
http://blog.csdn.net/pipisorry/article/details/52902432 ) 或var z = new Array[String](3) 以上语法中,z 声明一个 ...
- 阿里云手动安装特定版本的nginx
想添加nginx的缓存功能, 结果1.4.6还不支持. apt-get remove nginx 374 sudo apt-key add nginx_signing.key 375 deb http ...
- SceneKit做一个旋转的地球效果
SceneKit可以用寥寥几行帮你完成很多OpenGL复杂的3D设置代码,下面本猫就带大家完成一个旋转的3D地球的场景. 首先需要地球表面图片,将其导入到Xcode中: 我们用SceneKit内置的几 ...
- [django]用fastcgi部署
django官方已经开始弃用fastcgi来部署django应用了,作为以前使用过的用户,还是贴一个配置,用来做纪念吧.. 项目下 #! /bin/sh case "$@" in ...
- 通过JNDI从服务器容器中获取资源_Spring JNDI+Mysql+Tomcat
通过JNDI从服务器容器中获取DataSource资源 (由容器管理,不要关闭它,容器自己会处理)上一篇我们使用的是dbcp,这里使用JNDI: 使用JNDI连接数据: 在Spring可以注释 < ...
- 查看某一职责下对应的菜单&功能&请求
查看菜单&功能 SELECT res.RESPONSIBILITY_NAME 职责名称, menu.MENU_NAME 菜单编码, menu.USER_MENU_NAME 菜单名称, func ...
- Android初级教程短信防火墙
如果你有女神,而且有情敌的话,你看到这篇文章会有一种窃喜的感觉. 需求:对情敌的号码进行拦截,让女神手机永远收不到它的号码. 首先定义一个广播接收者类: package com.example.sms ...