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放到left左边2放到right右边,JAVA实现如下:

    public void sortColors(int[] nums) {
int left = 0, right = nums.length - 1;
for (int i = 0; i <= right;) {
if (nums[i] == 0 && i > left) {
int temp = nums[i];
nums[i] = nums[left];
nums[left] = temp;
left++;
} else if (nums[i] == 2 && i < right) {
int temp = nums[i];
nums[i] = nums[right];
nums[right] = temp;
right--;
} else
i++;
}
}

Java for LeetCode 075 Sort Colors的更多相关文章

  1. [Leetcode Week2]Sort Colors

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

  2. 【LeetCode】Sort Colors 数组排序

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

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

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

  4. 【LeetCode】Sort Colors

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

  5. 【LeetCode】075. Sort Colors

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

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

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

  7. [LeetCode题解]: Sort Colors

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given an a ...

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

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

  9. 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. ElasticSearch插件安装Head、Kopf与Bigdesk

    ElasticSearch-Head ElasticSearch-Head 是一个与Elastic集群(Cluster)相交互的Web前台. ES-Head的主要作用 它展现ES集群的拓扑结构,并且可 ...

  2. 如何使用lessc编译.less文件

    LESS :一种动态样式语言. LESS 将 CSS 赋予了动态语言的特性,如 变量, 继承, 运算, 函数. LESS 既可以在 客户端 上运行 (支持IE 6+, Webkit, Firefox) ...

  3. jQuery返回顶部代码组件

    非原创,拿来修改,样式可自定义,div,img都可以,效果如下: 下载地址:http://files.cnblogs.com/files/EasonJim/jquery.topback.rar 项目相 ...

  4. CoreOS Architecture Learning

    目录 . CoreOS简介 . CoreOS部署.安装.使用 . CoreOS命令使用 1. CoreOS简介 0x1: CoreOS和Docker的关系 我们先来看一张Docker的架构图

  5. Process manufacturing和Discrete manufacturing的区别

    Process manufacturing(Process industry) 加工制造,或者加工工业.其一个重要特征是,原材料被加工成成品后,我们再也无法将它恢复成原料,比如,苹果罐头,我们再没法把 ...

  6. groovy-语句

    groovy语句类似于java语句,但是在groovy中的分号”;”是可选的.比如: 1 def x = [1, 2, 3] 2 println x 3 def y = 5; def x = y +  ...

  7. SQL localdb 连接字符串

    http://blog.csdn.net/greystar/article/details/47699797 原来SQL 2012 下连接LOCALDB,字符串为: Data Source=(Loca ...

  8. JAVA敏捷开发环境搭建

    前面介绍了创业型软件公司的工作模式,这里详细介绍下如何实施,第一步是先要搭建环境,有了环境才能开展工作. 整个软件项目分为四个环境 开发本地环境.开发环境.测试环境.IDC环境.和传统C++开发不一样 ...

  9. php 非缓冲查询

    最近在开发一个PHP程序时遇到了下面的错误: PHP Fatal error: Allowed memory size of 268 435 456 bytes exhausted 错误信息显示允许的 ...

  10. WPF TreeView绑定字典集合

    <TreeView Name="Tree" HorizontalAlignment="Left" Height="269" Width ...