Sort Colors Java
来源:互联网 发布:软件升级必然凯文凯利 编辑:程序博客网 时间:2024/04/30 07:55
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.
Key to solve: 3 pointers
Since there are 3 distinguish type of colors
we can set up 3 additional pointers start from 0
Assign value by increasing order since we are going to sort array by increasing order
There are 3 cases:
0: assign all 3 colors
1: assign two colors
2: assign only one color
public class Solution { public void sortColors(int[] A) { if(A.length==0) return; int red=0, white=0,blue=0; for(int i=0;i<A.length;i++){ if(A[i]==0){ A[blue++]=2; A[white++]=1; A[red++]=0; }else if(A[i]==1){ A[blue++]=2; A[white++]=1; }else{ A[blue++]=2; } } }}
0 0
- [Leetcode] Sort Colors (Java)
- Sort Colors Java
- Sort Colors - Java
- leetcode Sort Colors java
- Sort Colors (Java)
- [LeetCode][Java] Sort Colors
- [letecode java ] Sort Colors
- leetcode:Sort Colors 【Java】
- Sort Colors leetcode java
- leetcode Sort Colors java实现
- 【Leetcode】Sort Colors in JAVA
- leetcode-java-75. Sort Colors
- 【leetcode】75. Sort Colors【java】
- [LeetCode] 75. Sort Colors java
- [leetcode]75. Sort Colors(Java)
- Sort Colors
- sort colors
- Sort Colors
- lua io详细操作
- Sun-Java语言编码规范
- RFI\RFP\RFQ的区别
- 重写view的onAttachedToWindow () 和 onDetachedFromWindow ()与Home键屏蔽,捕获,修改
- Intel Galileo笔记(四)windows on galileo的配置
- Sort Colors Java
- C++ 继承之访问权限
- Cocos2d-x 瓦块地图小游戏 (五) 城墙 与 吃瓜
- 小心骗子,买放心机票
- MyBatis Spring整合配置映射接口类与映射xml文件
- 网站是否要修改标题
- Afinal 框架 Afinal DB的学习
- OGNL使用小结
- 普通IO口模拟实现I2C通信及应用解析