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 ...
随机推荐
- thinkphp5+GatewayWorker+Workerman
项目地址 ttps://www.workerman.net/workerman-chat thinkphp5+GatewayWorker+Workerman聊天室,可以多人聊天,指定某个人进行聊天, ...
- Exception from HRESULT:
在MFC工程中,在类向导的时候,偶尔会遇到 "Exception from HRESULT:" 的问题,问题的原因可能是移动工程之类的操作破坏了工程的某些文件或者更改了某些路径的映 ...
- 牛客多校第五场 H subsequence 2 拓扑排序
题意: 给你长度最长为1000的字符串,这个字符串中最多有10种字母,每次给你两种字母,输出这两种字母在字符串中的相对位置,问你这个字符串原本是什么样子,如果不存在则输出-1 题解: 把整个字符串看作 ...
- 线程池_ThreadPool
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; ...
- 微信小程序 tabBar模板
tabBar导航栏 小程序tabBar,我们可以通过app.json进行配置,可以放置于顶部或者底部,用于不同功能页面的切换,挺好的... 但,,,貌似不能让动态修改tabBar(需求:通过switc ...
- 关于操作系统中英文切换的.po和.mo介绍
一.文件简介 .po文件,.mo文件,.pot文件是由gettext程序生成或者使用的源代码和编译结果. 1..pot文件 是一种模板文件,其实质与.po文件一样,其中包含了从源代码中提取所有的 ...
- centos7 搭建 php7 + nginx (2)
安装php php下载地址 # 避免出错,先安装下面 yum install libzip libzip-devel libxml2-devel openssl openssl-devel bzip2 ...
- 箭头函数报错:Add @babel/plugin-proposal-class-properties (https://git.io/vb4SL) to the 'plugins' section of your Babel config to enable transformation.
解决:根目录新建babel.config.js加入如下内容 module.exports = { presets: [ "@babel/preset-env", "@ba ...
- webstorm中使用git管理服务器上的代码——入门级
一.首先要确保电脑已经成功安装好git了.(记住git的安装位置) 二.这里需要给webstorm配置一下:依次点击:file –> Settings –> Version Control ...
- 转:Eclipse中设置编码的方式
来源:http://blog.csdn.net/jianw2007/article/details/3930915 如果要使插件开发应用能有更好的国际化支持,能够最大程度的支持中文输出,则最好使 Ja ...