leetcode 75 Sorted Colors
两种解法
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的更多相关文章
- LeetCode 75. Sort Colors (颜色分类):三路快排
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- [LeetCode] 75. Sort Colors 颜色排序
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- LeetCode 75. Sort Colors(排序颜色)
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- Leetcode 75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- [leetcode]75. Sort Colors三色排序
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- LeetCode 75 Sort Colors(颜色排序)
翻译 给定一个包括红色.白色.蓝色这三个颜色对象的数组.对它们进行排序以使同样的颜色变成相邻的,其顺序是红色.白色.蓝色. 在这里,我们将使用数字0.1和2分别来代表红色.白色和蓝色. 原文 Give ...
- leetCode 75.Sort Colors (颜色排序) 解题思路和方法
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- leetcode 75. Sort Colors (荷兰三色旗问题)
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- leetcode 75 Sort Colors 计数排序,三路快排
解法一:计数排序:统计0,1,2 的个数 时间复杂度:O(n) 空间复杂度:O(k) k为元素的取值范围, 此题为O(1) class Solution { public: void sortC ...
随机推荐
- Altera: set pin locations using tcl
1, compile the project; 2, store current tcl settings: Project –> Generate Tcl File from Project- ...
- 【JZOJ4474】【luoguP4071】排列计数
description 求有多少种长度为 n 的序列 A,满足以下条件: (1)1 ~ n 这 n 个数在序列中各出现了一次 (2)若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列恰好有 ...
- 校园商铺-2项目设计和框架搭建-9验证Service
1. 新建接口 main: com.csj2018.o2o.service/AreaService.java package com.csj2018.o2o.service; import java. ...
- thinkphp switch标签
用法: <switch name="变量" > <case value="值1" break="0或1">输出内容1 ...
- for in循环介绍以及陷阱
大家都知道在JavaScript中提供了两种方式迭代对象: (1)for 循环: (2)for..in循环: 使用for循环进行迭代数组对象,想必大家都已经司空见惯了.但是,使用for.. in循环时 ...
- python定时任务模块APScheduler
一.简单任务 定义一个函数,然后定义一个scheduler类型,添加一个job,然后执行,就可以了 5秒整倍数,就执行这个函数 # coding:utf-8 from apscheduler.sche ...
- golang中time包一个简单的时间格式输出
一.代码 package main import ( "fmt" "time" ) func main() { //"2006-01-02 15:04 ...
- Mysql保留字列表
Mysql保留字列表.吠品整理. 尝试使用一个识别符,例如使用嵌入式MySQL 数据类型或函数名作为表名或列名,例如TIMESTAMP 或GROUP,会造成一个常见问题.允许你这样操作( 例如,A ...
- RPC远程过程调用实例详解
1.创建IDL文件,定义接口. IDL文件可以由uuidgen.exe创建. 首先找到系统中uuidgen.exe的位置,如:C:\Program Files\Microsoft Visual Stu ...
- Systm.IO.File.cs
ylbtech-Systm.IO.File.cs 1.程序集 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5619 ...