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 ...
随机推荐
- Delphi 最小化窗体到托盘
---- 现在很多的应用程序都有这样一种功能,当用户选择最小化窗口时,窗口不是象平常那样最小化到任务栏上,而是“最小化”成一个任务栏图标.象FoxMail 3.0 NetVampire 3.0等都提供 ...
- webpack官方文档学习
一.webpack是什么? webpack是一款模块加载器兼打包工具,它能把各种资源,例如JS(含JSX).coffee.样式(含less/sass).图片等都作为模块来使用和处理. 二.安装 前提条 ...
- POJ 2104:K-th Number 整体二分
感觉整体二分是个很有趣的东西. 在别人的博客上看到一句话 对于二分能够解决的询问,如果有多个,那么如果支持离线处理的话,那么就可以使用整体二分了 树套树写了一天还是WA着,调得焦头烂额,所以决定学cd ...
- springboot下自定义配置文件,并在项目里读取的方法
首先 pom文件引入springboot文件处理器 <dependency> <groupId>org.springframework.boot</groupId> ...
- 用pymysql实现的注册登录公告练习
import pymysql #1.连接服务器 conn=pymysql.connect( host='127.0.0.1', port=3306, user='root', password='12 ...
- zepto问题
jq功能对照表 http://jsrun.it/21f/mrCH 不支持 jquery的 ajaxSetup 全局修改ajax的设置 $.ajaxSetup({beforeSend : beforeS ...
- MT Call来电流程分析????
- 第四篇:java读取Excel简单模板
场景:对于经常需要导入Excel模板或数据来解析后加以应用的,使用频率非常之高,做了一个比较稳定的版本,体现在这些地方工具:org.apache.poi使用前必须了解这些:1.要解析,那肯定先判断是不 ...
- 12_springmvc拦截器
一.定义 Spring Web MVC 的处理器拦截器类似于Servlet 开发中的过滤器Filter,用于对处理器进行预处理和后处理. 二.拦截器定义 实现HandlerInterceptor接口, ...
- UNIT对话系统(杂记)
单轮对话指标: 召回率=机器人能回答的问题数/问题总数 准确率=机器人正确回答的问题数/问题总数 问题解决率=机器成功解决的问题数/问题总数 多轮对话指标: 任务完成率=成功结束的多轮会话数/多轮会话 ...