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.

将数组中的0,1,2分别排序,使得相同的数字相邻,这个比较简单,直接看代码:

 class Solution {
public:
void sortColors(vector<int>& nums) {
int redNum = count(nums.begin(), nums.end(), );
int whiteNum = count(nums.begin(), nums.end(), );
int blueNum = count(nums.begin(), nums.end(), );
int sz = nums.size();
vector<int> ret(sz, );
int redCount = , whiteCount = , blueCount = ;
for(int i = ; i < sz; ++i){
if(nums[i] == ){
ret[redCount++] = ;
}else if(nums[i] == ){
ret[whiteCount + redNum] = ;
whiteCount++;
}else{
ret[blueCount + redNum + whiteNum] = ;
blueCount++;
}
}
nums = ret;
}
};

当时题目要求的事one-pass,const-space algorithm,当时硬是没想出来,现在看了下别人的解答,真简洁,原理很简单,3指针,画个图就能看懂了,代码如下所示:

 public class Solution {
public void sortColors(int[] nums) {
int i = 0;
int j = nums.length - 1;
int k = nums.length - 1;
while(i <= j){
if(nums[i] == 2){
int tmp = nums[k];
nums[k] = nums[i];
nums[i] = tmp;
k--;
if(k < j)
j = k;
}else if(nums[i] == 1){
int tmp = nums[j];
nums[j] = nums[i];
nums[i] = tmp;
j--;
}else{
i++;
}
}
}
}

LeetCode OJ:Sort Colors(排序颜色)的更多相关文章

  1. LeetCode 75. Sort Colors (颜色分类):三路快排

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  2. LeetCode 75 Sort Colors(颜色排序)

    翻译 给定一个包括红色.白色.蓝色这三个颜色对象的数组.对它们进行排序以使同样的颜色变成相邻的,其顺序是红色.白色.蓝色. 在这里,我们将使用数字0.1和2分别来代表红色.白色和蓝色. 原文 Give ...

  3. [LeetCode OJ] Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  4. 【LeetCode】Sort Colors 数组排序

    题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...

  5. [LeetCode] Wiggle Sort 摆动排序

    Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...

  6. [Leetcode Week2]Sort Colors

    Sort Colors题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/sort-colors/description/ Description Give ...

  7. 【LeetCode】Sort Colors

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  8. LeetCode 75. Sort Colors(排序颜色)

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  9. [LeetCode] 75. Sort Colors 颜色排序

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  10. leetCode 75.Sort Colors (颜色排序) 解题思路和方法

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

随机推荐

  1. 011-git-将tag推送到远端

    1.将tag推送到远端 在使用TortoiseGit过程中,push推送过程中,选择include tag,远端就有次分支.

  2. Python爬虫:获取新浪网新闻

    代码 #coding:utf-8 import requests from bs4 import BeautifulSoup res = requests.get("http://news. ...

  3. CuteEditor.Editor+a+a+c+a+a.a() System.RuntimeType.get_Assembly() 问题解决方法

    问题: Server Error in '/' Application. Attempt by method 'CuteEditor.Editor+a+a+c+a+a.a()' to access m ...

  4. LeetCode:对角线遍历【498】

    LeetCode:对角线遍历[498] 题目描述 给定一个含有 M x N 个元素的矩阵(M 行,N 列),请以对角线遍历的顺序返回这个矩阵中的所有元素,对角线遍历如下图所示. 示例: 输入: [ [ ...

  5. offsetHeight+scrollHeight+clientHeight

    ch 窗口可见区域高度 :ch = padding + height(height不是所有内容的高度,是样式定义的高度) oh border以内的内容高度: oh = border + padding ...

  6. jsp导出

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  7. GZDBHelper

    NuGet:GZDBHelper 初始化: public class APIBase : ApiController { protected GZDBHelper.IDatabase db; publ ...

  8. JSP Cookie状态管理

    JSP中创建与使用Cookie 创建Cookie对象 Cookie newCookie = new Cookie(String key, Object value); 写入Cookie对象 respo ...

  9. nginx常见面试题1

    Nginx是网页服务器运维人员不可能绕开的一个弯,剩下几个比较高危的面试范围是:linux基础.网络知识基础.python,或许还会有zabbix等监控工具.这里先说nginx,后面几个肯定也会写. ...

  10. Luogu-3222 [HNOI2012]射箭

    几何题,二次函数,化一下式子吧 设二次函数\(y=ax^2+bx\),对于一个线段\((x,y1)\),\((x,y2)\),与他相交的条件是\(y1<=ax^2+bx<=y2\) 对于\ ...