LeetCode: Sorted Color
Title:
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(vector<int>& nums) {
        int *c = new int [];
        memset(c,,sizeof(int)*);
        for (int i = ; i < nums.size(); i++){
            c[nums[i]]++;
        }
        nums.clear();
        for (int i =  ; i < ; i++){
            for (int j =  ; j < c[i]; j++){
                nums.push_back(i);
            }
        }
    }
};
也可以遍历一次就能得到结果,使用3个下标,分别指向0,1,2对应的下标位置。可以这么理解,最左边是0,最右边是2,中间遇到1不用管
class Solution {
public:
    void sortColors(int A[], int n) {
        if(n <= ) return;
        int start = -, end = n;
        int p = ;
        while(p < n && start < end){
            if(A[p] == ){
                if(p > start) swap(A, ++start, p);
            }else if(A[p] == ){
                if(p < end) swap(A, p, --end);
            }else ++p;
        }
    }
private:
    void swap(int A[], int i, int j){
        int temp = A[i];
        A[i] = A[j];
        A[j] = temp;
    }
};
LeetCode: Sorted Color的更多相关文章
- 【Python】列表~深入篇
		
列表是一列按特定顺序排列的元素组成. 在Python中,用方括号[]来表示列表 下面是一个语言列表 Language = ['Chinese','English','Franch','Deutsch' ...
 - [LeetCode] 800. Similar RGB Color 相似的红绿蓝颜色
		
In the following, every capital letter represents some hexadecimal digit from 0 to f. The red-green- ...
 - Leetcode: Convert sorted list to binary search tree (No. 109)
		
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
 - [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
		
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
 - [LeetCode] Two Sum II - Input array is sorted 两数之和之二 - 输入数组有序
		
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
 - [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
		
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
 - [LeetCode] Find Minimum in Rotated Sorted Array  寻找旋转有序数组的最小值
		
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
 - [LeetCode]  Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树
		
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
 - [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树
		
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...
 
随机推荐
- 【机器学习】BP神经网络实现手写数字识别
			
最近用python写了一个实现手写数字识别的BP神经网络,BP的推导到处都是,但是一动手才知道,会理论推导跟实现它是两回事.关于BP神经网络的实现网上有一些代码,可惜或多或少都有各种问题,在下手写了一 ...
 - Candy
			
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
 - zoj 2974 Just Pour the Water (矩阵快速幂,简单)
			
题目 对于案例的解释请见下图: 这道要变动提取一下矩阵,之后就简单了 具体解释可看代码: #include <string.h> #include <stdio.h> #inc ...
 - Android开发--解决AndroidADT开发工具不能代码提示的问题
			
google android的新的开发工具,打开以后没有代码自动提示功能,下面对ADT工具的一些配置: 1.设置代码的字体 设置JAVA文件代码的字体:我这里设置的14 常规.
 - POJ 1724 ROADS(BFS+优先队列)
			
题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...
 - pycharm 基础教程
			
pycharm 教程(一)安装和首次使用 PyCharm 是我用过的python编辑器中,比较顺手的一个.而且可以跨平台,在macos和windows下面都可以用,这点比较好. 首先预览一下 PyCh ...
 - Oracle 6 - 锁和闩 - 锁类型
			
Oracle锁大类 1.DML锁 (SELECT, INSERT, UPDATE, DELETE, MERGE是对数据库加的锁, 可能是行锁,也可能是表锁) 2.DDL锁 (Create, Alter ...
 - C#五层架构
			
UI Business Logic Layer Business Rule Layer Data Access Layer Data Definition Layer 简单工厂模式 simple fa ...
 - Ios 弹框 MJPopup,KxMenu
			
IOS 弹框 如果直接弹出一个自定义的视图 可以选用第三方: MJPopup 弹出: if(!bandview) { bandview=[[[NSBundle mainBundle]loadNibNa ...
 - java c# 加密与解密对照
			
原文 java c# 加密与解密对照 最近一直烦恼,java , c# 加密的不同,然后整理了一下,留个备份的轮子: 其中在 java.c#加密转换时,最重要的是 IV 的确定,我常常用如下方法使得j ...