75. Sort Colors
来源:互联网 发布:mac版的easysketch 编辑:程序博客网 时间:2024/06/14 11:46
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.
思路:这题要实现的是将所有的0放到数组的左边,将所有的2放到数组的右边,采取快速排序的思想,将2都交换到数组的后面,将0都交换到数组的前面,具体代码如下:
public void sortColors(int[] nums) { if (nums.length==0||nums.length==1) return; int end=nums.length-1; int beign=0; for (int i = 0; i <=end; i++) { while (nums[i]==2&i<end) { int temp=nums[i]; nums[i]=nums[end]; nums[end--]=temp; } while (nums[i]==0&&i>beign){ int temp=nums[i]; nums[i]=nums[beign]; nums[beign++]=temp; } } }
阅读全文
0 0
- 【sort专题】75. Sort Colors
- 75. Sort Colors
- [LeetCode]75.Sort Colors
- LeetCode --- 75. Sort Colors
- [Leetcode] 75. Sort Colors
- [leetcode] 75.Sort Colors
- 75. Sort Colors
- 【leetcode】75. Sort Colors
- 75. Sort Colors
- [leetcode] 75. Sort Colors
- Leetcode 75. Sort Colors
- *LeetCode 75. Sort Colors
- 75. Sort Colors LeetCode
- 75. Sort Colors
- Leetcode 75. Sort Colors
- 75. Sort Colors
- [LeetCode]75. Sort Colors
- 75. Sort Colors
- GitHub详解(2)
- 在迁移到HTTPS之前需要考虑的事情
- C++ 标准库之iomanip
- 机器人(n舵机)电源解决方案
- 聊聊前端开发中关于闭包的那点事
- 75. Sort Colors
- Microsoft Access、MySQL 以及 SQL Server 所使用的数据类型和范围
- spring基本概念
- web工程师眼中的MVC模式
- 模拟实现C语言中的库函数atoi
- 本地主机与虚拟主机之间互传文件
- 突发长度(burst length)
- 初识比特币——比特币私钥
- 一头扎进《快速排序》