LeetCode 75.Sort Colors
来源:互联网 发布:评价成都软件技术学院 编辑:程序博客网 时间:2024/06/07 22:54
description:
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.
可以使用双指针的算法来进行计算,但是这用因为程序就只有3个变量,因此可以使用特定的判断来进行运算。
public class Solution { public void sortColors(int[] nums) { if (nums == null || nums.length < 1) { return; } int left = 0; int right = nums.length - 1; int i = 0; while (i <= right) { if (nums[i] == 0) { swap(nums, left, i); left++; i++; } else if (nums[i] == 1) { i++; } else { swap(nums, i, right); right--; } } } private void swap(int[] nums, int left, int right) { int temp = nums[left]; nums[left] = nums[right]; nums[right] = temp; }}
0 0
- [LeetCode]75.Sort Colors
- LeetCode --- 75. Sort Colors
- [Leetcode] 75. Sort Colors
- [leetcode] 75.Sort Colors
- 【leetcode】75. Sort Colors
- [leetcode] 75. Sort Colors
- Leetcode 75. Sort Colors
- *LeetCode 75. Sort Colors
- 75. Sort Colors LeetCode
- Leetcode 75. Sort Colors
- [LeetCode]75. Sort Colors
- LeetCode 75. Sort Colors
- LeetCode *** 75. Sort Colors
- leetcode 75. Sort Colors
- LeetCode 75. Sort Colors
- leetcode-75. Sort Colors
- [leetcode]75. Sort Colors
- LeetCode-75.Sort Colors
- html5基础项目文件分布
- C# 读取写入数据到ACCESS数据库基于FRAMEWORK2.0
- php读取csv内容,中文不显示问题
- hdu1159(dp)最长公共子序列
- ISO-8859-1和Windows-1252的区别
- LeetCode 75.Sort Colors
- com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:的解决
- Lucene原理与应用
- linux进程通信的方式、进程的8种状态、线程通信的方式、线程的4种状态
- 内存管理
- 挖矿程序minerd入侵分析和解决办法
- 基于Android 的GPS 移植(主要结构体及接口介绍)
- 第一次面试问题总结
- vim配色方案设置的几个问题