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. 【CodeForces 599A】D - 特别水的题4- Patrick and Shopping

    Description  meter long road between his house and the first shop and a d2 meter long road between h ...

  2. int方法

    代码 #int内部功能 name='Kamil.Liu' age=18 num=-11 print(dir(age)) print(age.bit_length())#返回表示当前数字占用的最少位数 ...

  3. SOM自组织映射网络 教程

    概述 SOM是芬兰教授Teuvo Kohonen提出的一种神经网络算法,它提供一种将高维数据在低维空间进行表示的方法(通常是一维或二维).缩减向量维度的过程,叫做向量量化(vector quantis ...

  4. JavaScript parser

    JavaScript parser 和上面功能有点像,折叠JS代码,快速找到JS中类,方法的工具

  5. poj 1743 二分答案+后缀数组 求不重叠的最长重复子串

    题意:给出一串序列,求最长的theme长度 (theme:完全重叠的子序列,如1 2 3和1 2 3  or  子序列中每个元素对应的差相等,如1 2 3和7 8 9) 要是没有差相等这个条件那就好办 ...

  6. Git删除文件操作

    使用Git删除文件需要使用Git rm命令来实现,最后git commit 需要注意的是直接rm命令删除后是不可以的,可以用git status 命令尝试一下,效果如图下(创建了test文件,演示了g ...

  7. 使用easyUI 创建复杂的toolbar到datagrid

    http://www.cnblogs.com/javaexam2/archive/2012/08/10/2632649.html @author YHC datagrid 的toolbar能包含的不仅 ...

  8. Linux无法使用userdel删除用户和组的解决办法

    转自:http://www.linuxidc.com/Linux/2013-07/87371.htm 简述: 今天在看书的时候,看到有个实例,手痒痒的跟着做了起来...但是,出现问题了..测试的用户和 ...

  9. You should blog even if you have no readers

    Spencer Fry wrote a great post on "Why entrepreneurs should write." I would further add th ...

  10. Entity Framework DataAnnotations

    前言 DataAnnotation 特性由.NET 3.5中引进,给.NET中的类提供了一种添加验证的方式.但是在EF中它又可以对映射关系进行控制,相比较Fluent API使用起来要简单一些. Da ...