【leetcode】Sort Colors
来源:互联网 发布:手机数据备份怎么弄 编辑:程序博客网 时间:2024/06/02 04:07
Sort Colors
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.
由于只有三个数字,因此简单记录下每个数字出现的次数,然后重新构造下队列即可。
class Solution {public: void sortColors(int A[], int n) { int num0=0,num1=0,num2=0; for(int i=0;i<n;i++) { if(A[i]==0) { num0++; } if(A[i]==1) { num1++; } if(A[i]==2) { num2++; } } for(int i=0;i<num0;i++)A[i]=0; for(int i=num0;i<num0+num1;i++)A[i]=1; for(int i=num0+num1;i<num0+num1+num2;i++)A[i]=2; }};
0 0
- LeetCode: Sort Colors
- LeetCode Sort Colors
- LeetCode : Sort Colors
- leetcode 77: Sort Colors
- Leetcode 75 Sort Colors
- leetcode 101: Sort Colors
- [LeetCode]Sort Colors
- [leetcode]Sort Colors
- LeetCode-Sort Colors
- [leetcode] Sort Colors
- [LeetCode] Sort Colors
- [leetCode] Sort Colors
- LeetCode - Sort Colors
- leetcode:Sort Colors
- LeetCode | Sort Colors
- 【LeetCode】Sort Colors
- Leetcode: Sort Colors
- [LeetCode] Sort Colors
- iOS-变量的property属性设置和意义总结
- 如何配置Tomcat的JNDI (以oracle数据库为例子)
- 2.29习题
- tp执行流程
- Ant打可执行jar包指南
- 【leetcode】Sort Colors
- 蛋疼的Layout 和 分辨率适配
- 解决"要执行请求的操作,WordPress需要访问您网页服务器的权限"
- PSQL Doc ...
- linux网络配置
- 2.29
- 通过httpClient获取重定向后的页面链接地址(应用与百度链接跳转获取原网页的链接地址)
- 【leetcode】Swap Nodes in Pairs
- #!/bin/sh與#!/bin/bash的區別