Question

75. Sort Colors

Solution

题目大意:

给一个数组排序,这个数组只有0,1,2三个元素,要求只遍历一遍

思路:

记两个索引,lowIdx初始值为0,highIdx初始值为nums.length - 1,遍历数组,如果是2就与highIdx处元素互换且highIdx—,如果是0就与lowIdx处元素互换且lowIdx++,i++,还剩一种情况就是1了,执行i++,循环结束条件是i<=highIdx

Java实现:

public void sortColors(int[] nums) {
int lowIdx = 0;
int highIdx = nums.length - 1;
int i = 0;
while (i <= highIdx) {
if (nums[i] == 2) {
int tmp = nums[highIdx];
nums[highIdx--] = nums[i];
nums[i] = tmp;
} else if (nums[i] == 0) {
int tmp = nums[lowIdx];
nums[lowIdx++] = nums[i];
nums[i++] = tmp;
} else {
i++;
}
}
}

75. Sort Colors - LeetCode的更多相关文章

  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. 75. Sort Colors(颜色排序) from LeetCode

      75. Sort Colors   给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...

  3. 刷题75. Sort Colors

    一.题目说明 题目75. Sort Colors,给定n个整数的列表(0代表red,1代表white,2代表blue),排序实现相同颜色在一起.难度是Medium. 二.我的解答 这个是一个排序,还是 ...

  4. 【LeetCode】75. Sort Colors (3 solutions)

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  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(排序颜色)

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

  7. 【一天一道LeetCode】#75. Sort Colors

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  8. 【LeetCode】75. Sort Colors 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 计数排序 双指针 日期 题目地址:https://l ...

  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. 二十、生成BOM表

  2. 推荐一款强大的轻量级模块化WEB前端快速开发框架--UIkit

    前言 今天给大家分享一款强大的轻量级模块化WEB前端快速开发框架--UIkit 到目前(2016-06-20)为止,UIkit在github上的Forks已达到了1350个,而Stars更是达到了69 ...

  3. ubantu14.04系统设置无法正常使用

    方法一:执行命令: sudo apt-get install ubuntu-desktop方法二:如果系统设置打不开,请重新安装gnome-control-centersudo apt-get ins ...

  4. Git使用方法以及出现的bug解决方案

    git常用命令 1.本地库初始化: git init 2.设置签名 (1)项目级别(项目里面) git config user.name xxx git config user.email xxx ( ...

  5. spring-注入list集合对象(值是对象)

    1.创建stu类 public class Stu { // //1.数组类型 // private String[] courses; // // //2.list集合属性 // private L ...

  6. Intellij IDEA中查看字节码

    首先安装插件,这俩都勾上 Intellij IDEA 直接集成了一个工具菜单,可以直接查看字节码,打开 ByteCode 插件窗口方法如下:

  7. 01 | 堆、栈、RAII:C++里该如何管理资源?(极客时间笔记)

    基本概念 堆,英文是 heap,在内存管理的语境下,指的是动态分配内存的区域.这个堆跟数据结构里的堆不是一回事.这里的内存,被分配之后需要手工释放,否则,就会造成内存泄漏. C++ 标准里一个相关概念 ...

  8. 虚拟机VMware的安装与Xshell的应用

    先安装VMware 1.安装就按照提示一点点安装就行了 配置网络 打开VMware 这里的IOS映像文件在https://developer.aliyun.com/mirror/里下载 这里用方向键往 ...

  9. switch 用法

    1.语法格式和规则 switch case 语句语法格式如下: switch(expression){ case value : //语句 break; //可选 case value : //语句 ...

  10. jdk-1.8环境变量配置

    1.首先下载好jdk-1.8的安装包. 这个安装也是傻瓜式安装,一直下一步即可.一定要记得中间你所设置的安装路径 2.切记 切记 jdk的安装路径 ! 3.右键"此电脑",点击最下 ...