sort colors
来源:互联网 发布:人工智能工程师 编辑:程序博客网 时间:2024/06/05 16:06
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.
class Solution {public: void sortColors(int A[], int n) { if(n <= 1) return; int start = -1, end = n; int p = 0; while(p < n){ if(A[p] == 0){ if(p > start) swap(A, ++start, p); else ++p; }else if(A[p] == 2){ if(p < end) swap(A, p, --end); else ++p; }else ++p; } }private: void swap(int A[], int i, int j){ int temp = A[i]; A[i] = A[j]; A[j] = temp; }};
0 0
- Sort Colors
- sort colors
- Sort Colors
- Sort Colors
- Sort Colors
- Sort Colors
- Sort Colors
- Sort Colors
- Sort Colors
- Sort Colors
- Sort Colors
- Sort Colors
- Sort Colors
- Sort Colors
- Sort Colors
- Sort Colors
- Sort Colors
- Sort Colors
- MyEclipse的实体关系设计
- Android开发,eclipse的LogCat使用技巧
- JVM GC总结
- C# 30分钟LINQ教程
- TabHost 缓存
- sort colors
- 关于android:focusable属性 - daishuguang
- 黑马程序员----多线程技术理解
- 字符串转换成int数组
- C# 关于匿名类型的特性
- Sudoku Solver
- Invocation of init method failed; nested exception is org.apache.ibatis.binding.BindingException: In
- oracle之约束
- Android小知识5