【LeetCode】数组系列(排序)
来源:互联网 发布:迪奥华氏知乎 编辑:程序博客网 时间:2024/05/11 21:07
75. Sort Colors
题目:由0,1,2组成的数组,对其进行排序。没搞懂考啥
public class Solution { public void sortColors(int[] nums) { int count0 = 0, count1 = 0, count2 = 0; for(int x : nums){ if(x == 0) count0++; else if(x == 1) count1++; else count2++; } for(int i = 0; i < nums.length; i++){ if(i < count0) nums[i] = 0; else if(i < count0+count1) nums[i] = 1; else nums[i] = 2; } return; }}
一次遍历的方法
public class Solution { public void sortColors(int[] nums) { int start = 0, end = nums.length-1; for(int i = 0; i <= end; i++){ if(nums[i] == 0){ int temp = nums[i]; nums[i] = nums[start]; nums[start++] = temp; } else if(nums[i] == 2){ int temp = nums[i]; nums[i--] = nums[end]; nums[end--] = temp; } } return; }}
阅读全文
0 0
- 【LeetCode】数组系列(排序)
- 【LeetCode】数组系列(间隔)
- 【LeetCode】数组系列(去重)
- 【LeetCode】数组系列(子序列)
- leetcode系列(2)链表排序
- 【LeetCode】链表系列(排序)
- 【LeetCode】查找数组系列
- 【LeetCode】排序系列
- 数组排序系列 sortedArrayUsingComparator
- 数组排序系列 sortedArrayUsingComparator
- LeetCode 455. Assign Cookies(数组,排序)
- 【LeetCode】Sort Colors 数组排序
- 列表重新排序——Leetcode系列(八)
- LeetCode 88 Merge Sorted Array(合并排序数组)(*)
- 【leetcode】在旋转排序数组中查找(二分)
- 【leetcode】有重复的旋转排序数组查找(二分)
- 使用快速排序算法对列表进行排序——Leetcode系列(四)
- 使用插入排序算法对列表进行排序——Leetcode系列(五)
- Java子线程和主线程交替输出(一个简单的实例)
- android studio解决finished with non-zero exit value 1方法
- ObjectArx编程(一) ------ 环境的搭建
- Kali Linux安装ipython2 和 ipython3
- Ubuntu14.10登录界面隐藏其他用户登录窗口
- 【LeetCode】数组系列(排序)
- Hibernate基础的增删改查
- 那些年——2 心声共鸣
- 阿里云 nginx
- 排序(3)直接插入排序
- tabbaritem 图片太大解决方案
- 运行项目报错-Manifest merger failed with multiple errors, see logs
- Linux 更改ssh 端口
- 【区间DP】【二维线段树】【二维树状数组】2017.5.20 T3 deliver 题解