两种解法

1)记录0和1的个数

然后按照记录的个数将0和1重新放入原数组,剩下的补2

2)双指针left,right

left表示0~left-1都为0,即i之前都为0

right表示right+1~nums.length都为2,即j之后都为2

遍历原数组

  a)遇到为0的就把当前nums[i]与nums[left]交换

  b)遇到为2的就交换nums[i]&nums[right],注意,写代码的时候要考虑交换过来的nums[right]有可能是2,如果i正常迭代变成i+1,漏掉了nums[right]为2的情况,所以我们这里i要减1

那么为什么前面a)不需要i减一呢?因为按照我们对left的定义,nums[left]不可能为0

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

leetcode 75 Sorted 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 颜色排序

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

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

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

  4. Leetcode 75. Sort Colors

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

  5. [leetcode]75. Sort Colors三色排序

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

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

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

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

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

  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 计数排序,三路快排

    解法一:计数排序:统计0,1,2 的个数 时间复杂度:O(n) 空间复杂度:O(k)    k为元素的取值范围, 此题为O(1) class Solution { public: void sortC ...

随机推荐

  1. leetcode-11-盛水最多的容器

    题目描述: 方法一:双指针 class Solution: def maxArea(self, height: List[int]) -> int: left = 0 right = len(h ...

  2. (转)SQL盲注攻击的简单介绍

    转:http://hi.baidu.com/duwang1104/item/65a6603056aee780c3cf2968 1 简介     1.1 普通SQL注入技术概述     目前没有对SQL ...

  3. class.forname & classloader

    From https://www.cnblogs.com/gaojing/archive/2012/03/15/2413638.html 传统的使用jdbc来访问数据库的流程为: Class.forN ...

  4. VS2010-MFC(MFC常用类:CFile文件操作类)

    转自:http://www.jizhuomi.com/software/234.html CFile类概述 如果你学过C语言,应该知道文件操作使用的是文件指针,通过文件指针实现对它指向的文件的各种操作 ...

  5. laravel装饰者模式例子

    interface Decorator{ public function display(); } class XiaoFang implements Decorator { private $nam ...

  6. Django之跨表查询——正反向查询(ForeignKey)

    1.正向查询和反向查询: 外键的查询操作: 正向查询: # 正向查询 # 基于对象,跨表查询 book_obj = models.Book.objects.all().first() ret = bo ...

  7. centos zabbix4.0编译安装

    zabbix的部署原理 zabbix server需要把监控数据入sql数据库,所以得Mysql环境 zabbix的web是基于php开发的,所以得LNMP环境 部署zabbix server和zab ...

  8. [记录]学习树莓派3B接DHT11和LCD1602和修改树莓派时区

    前提 树莓派系统安装好 apache web 服务器,如未安装,可在树莓派内执行sudo apt-get install apache2 进行安装apache 也可以通过命令获取GPIO信息: gpi ...

  9. ie中onclick问题

    代码:<button > <span onclick="xxx();">确定</span></button> 在chrome和fir ...

  10. unordered_map

    #include <iostream> #include <cstdio> #include <queue> #include <algorithm> ...