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 ...
随机推荐
- BZOJ-1061 志愿者招募 线性规划转最小费用最大流+数学模型 建模
本来一眼建模,以为傻逼题,然后发现自己傻逼...根本没想到神奇的数学模型..... 1061: [Noi2008]志愿者招募 Time Limit: 20 Sec Memory Limit: 162 ...
- BZOJ-1070 修车 最小费用最大流+拆点+略坑建图
1070: [SCOI2007]修车 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 3624 Solved: 1452 [Submit][Status] ...
- 【uoj2】 NOI2014—起床困难综合症
http://uoj.ac/problem/2 (题目链接) 题意 给出n个操作包括And,or,xor,求从0~m中的一个数使得经过这些操作后得到的值最大. Solution 大水题..贪心由高到低 ...
- Codevs1299 切水果
题目描述 Description 简单的说,一共N个水果排成一排,切M次,每次切[L,R]区间的所有水果(可能有的水果被重复切),每切完一次输出剩下水果数量 数据已重新装配,不会出现OLE错误 时限和 ...
- hihocoder 1169 猜数字
传送门 时间限制:10000ms 单点时限:5000ms 内存限制:256MB 描述 你正在和小冰玩一个猜数字的游戏.小冰首先生成一个长为N的整数序列A1, A2, …, AN.在每一轮游戏中,小冰会 ...
- TIME_WAIT过多
Linux系统下,TCP/IP连接断开后,会以TIME_WAIT状态保留一定的时间,然后才会释放端口.当并发请求过多的时候,就会产生大量的 TIME_WAIT状态的连接,无法及时断开的话,会占用大量的 ...
- UML用例图总结
用例图主要用来描述“用户.需求.系统功能单元”之间的关系.它展示了一个外部用户能够观察到的系统功能模型图. [用途]:帮助开发团队以一种可视化的方式理解系统的功能需求. 用例图所包含的元素如下: 1. ...
- 利用sourcemap来调试sass
最近项目用上了sass,作为css的预处理器,它可以让我们用程序化的思维书写样式,极大的简化了css的开发,实在是前端居家旅行必备的利器. 我们都知道,在项目中,样式的频繁调试是不可避免的,用上sas ...
- 高效使用git的一些命令
1,添加文件到版本库 添加单个文件: git add filename 添加所有txt文件: git add *.txt 添加所有修改文件: git add ...
- apue第四章学习总结
apue第四章学习总结 4.1.若以stat函数去替换lstat函数,会发生: 原来的目录路径: $:~/workspace/apue2/include$ ls -l apue.h abc lrwxr ...