Java for LeetCode 075 Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
Note:
You are not suppose to use the library's sort function for this problem.
解题思路:
快排速度太慢,不如直接用两个指针进行标记,0放到left左边2放到right右边,JAVA实现如下:
- public void sortColors(int[] nums) {
- int left = 0, right = nums.length - 1;
- for (int i = 0; i <= right;) {
- if (nums[i] == 0 && i > left) {
- int temp = nums[i];
- nums[i] = nums[left];
- nums[left] = temp;
- left++;
- } else if (nums[i] == 2 && i < right) {
- int temp = nums[i];
- nums[i] = nums[right];
- nums[right] = temp;
- right--;
- } else
- i++;
- }
- }
Java for LeetCode 075 Sort Colors的更多相关文章
- [Leetcode Week2]Sort Colors
Sort Colors题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/sort-colors/description/ Description Give ...
- 【LeetCode】Sort Colors 数组排序
题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,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 ...
- 【LeetCode】Sort Colors
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 【LeetCode】075. 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题解]: Sort Colors
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given an a ...
- [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 ...
随机推荐
- TinyMCE(富文本编辑器)
[转]TinyMCE(富文本编辑器)在Asp.Net中的使用方法 官网演示以及示例代码:https://www.tinymce.com/docs/demo/image-tools/ 转自:http:/ ...
- Yii2修改默认布局
public $layout = 'layout';//在类中定义一个变量,名为$layout的php文件 <?php echo $content; ?>
- Mac OS X系统下编译运行C代码
1.使用编译器将源文件中的代码转换为二进制代码,这个过程叫做编译. 将终端的工作路径切换到源文件所在的路径. cc -c 源文件的名称.例如:cc -c main.c 如果没有意外的话,就会在当前工作 ...
- 解决Oracle忘记密码问题
在使用ORACLE的过程中,会出现各种各样的问题,各种各样的错误,其中ORA-12899就是前段时间我在将数据导入到我本地机器上的时候一直出现的问题.不过还好已经解决了这个问题,现在分享一下,解决方案 ...
- 解决SSH无密码登陆后又需要密码登陆
主节点CentOS_Master 从节点Slave_1. 我想着可能是 /etc/ssh/sshd_config下的那个公钥文件路径不对,看了下home/hxsyl/.ssh/authorized_k ...
- BZOJ4241 历史研究
Description IOI国历史研究的第一人——JOI教授,最近获得了一份被认为是古代IOI国的住民写下的日记.JOI教授为了通过这份日记来研究古代IOI国的生活,开始着手调查日记中记载的事件. ...
- poj 1743 二分答案+后缀数组 求不重叠的最长重复子串
题意:给出一串序列,求最长的theme长度 (theme:完全重叠的子序列,如1 2 3和1 2 3 or 子序列中每个元素对应的差相等,如1 2 3和7 8 9) 要是没有差相等这个条件那就好办 ...
- C#图片读取和保存
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- FluentData,它是一个轻量级框架,关注性能和易用性。
http://www.cnblogs.com/zengxiangzhan/p/3250105.html FluentData,它是一个轻量级框架,关注性能和易用性. 下载地址:FlunenData.M ...
- 代码重构-2 简单不变的 if else 用字典代替
原代码 private string GetExDesc(string lotteryCode) { string exDesc = "抽奖"; if (lotteryCode.T ...