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. 这道 ...
随机推荐
- Ioc 比较
public interface IC { } public class A { IC ic_; public A(IC ic) { ic_ = ic; } } public class B : IC ...
- failed creating the Direct3d device--debug
D3DDEVTYPE_REF 使用REF设备,用软件模拟Direct3D API 照理说是为了让电脑能跑本机不能硬件执行的渲染命令的 但我 pDeviceSettings->d3d9.Devic ...
- .net framework 源码调试 与 问题解决
调试方式有二种, 看官方资料就OK. 官方地址: http://referencesource.microsoft.com/serversetup.aspx 1. 使用配置在线地址安装 2. 下载安装 ...
- 【锋利的JQuery-学习笔记】遮罩层
效果图: 鼠标移动到上面后---> html: <div id="jnBrandList"> <ul> <li> <a href= ...
- jquery 父页面 子页面 同级页面 调用
项目中用到 代码 ,先 展示 ,以便以后用到,直接看下就会明白. var li_divs = parent.$("#servicelist")[0].contentWindow. ...
- WinForm 资源文件的使用
1. 创建资源文件: 2.双击资源文件,打开如下图:添加一个字符串: 名称为cnnstr 值为-- 3.添加文本文件和图像 4. 调用代码 MessageBox.Show(Resource1.cnns ...
- Java中finalize()
垃圾回收器要回收对象的时候,首先要调用这个类的finalize方法(你可以 写程序验证这个结论),一般的纯Java编写的Class不需要重新覆盖这个方法,因为Object已经实现了一个默认的,除非我们 ...
- HDU 3501 Calculation 2 (欧拉函数)
题目链接 题意 : 求小于n的数中与n不互质的所有数字之和. 思路 : 欧拉函数求的是小于等于n的数中与n互质的数个数,这个题的话,先把所有的数字之和求出来,再减掉欧拉函数中所有质数之和(即为eula ...
- Android 虚拟机安装SD卡
在cmd命令行下,进入platform-tools目录下. 1.创建sdcard mksdcard -l mycard 256M E:\android\myCards\mysdcard.img ...
- Android 打开闪光灯(手电筒)
package com.example.openBackLight; import android.app.Activity; import android.hardware.Camera; impo ...