Leetcode_sort-colors(c++ and python updated)
来源:互联网 发布:犇犇网络 编辑:程序博客网 时间:2024/05/06 06:22
地址:http://oj.leetcode.com/problems/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.
click to show follow up.
Follow up:
A rather straight forward solution is a two-pass algorithm using counting sort.
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.
Could you come up with an one-pass algorithm using only constant space?
参考代码:
class Solution {public: void sortColors(int A[], int n) { int left = 0, right = n-1; for(int i = 0; i < n && left<right;) { if(A[i]==0) { if(i>left) { A[i]=A[left]; A[left++] = 0; } else ++i; } else if(A[i]==2) { if(i<right) { A[i]=A[right]; A[right--]=2; } else ++i; } else ++i; } }};
SECOND TRIAL
c++:
class Solution {public: void sortColors(int A[], int n) { int i = 0, j = n-1, k=0; while(k<=j) { if(A[k]==0) swap(A[i++], A[k]); else if(A[k]==2) swap(A[j--], A[k]); else ++k; if(i>k) k = i; } }};
python:
class Solution: # @param A a list of integers # @return nothing, sort in place def sortColors(self, A): k = i = 0 j = len(A)-1 while k<=j: if not A[k]: A[i], A[k] = A[k], A[i] i+=1 elif A[k]==2: A[j], A[k] = A[k], A[j] j-=1 else: k+=1 if i>k: k = i
//Thrid trialclass Solution {public:void sortColors(int A[], int n) {int cur = 0, left = 0, right = n-1;while(cur<=right){if(A[cur]==0)swap(A[left++], A[cur]);else if(A[cur]==2)swap(A[right--], A[cur]);else++cur;if(left > cur)cur = left;}}};
0 0
- Leetcode_sort-colors(c++ and python updated)
- LeetCode_Sort Colors
- Leetcode_plus-one(c++ and python updated)
- Leetcode_pascals-triangle (updated c++ and python version)
- Leetcode_unique-paths (updated c++ and python version)
- Leetcode_path-sum (updated c++ and python version)
- Leetcode_jump-game (updated c++ and python version)
- Leetcode_valid-number(updated c++ and python version)
- Leetcode_multiply-strings (updated c++ and python version)
- Mixing Objective-C, C++ and Objective-C++: an Updated Summary
- Mixing Objective-C, C++ and Objective-C++: an Updated Summary
- CodeForces 673C Bear and Colors
- CodeForces 673C - Bear and Colors(模拟)
- Leetcode_maximum-depth-of-binary-tree (updated c++ and python version)
- Leetcode_binary-tree-level-order-traversal (updated c++ and python version)
- Leetcode_remove-duplicates-from-sorted-list (updated c++ and python version)
- Leetcode_merge-sorted-array(updated c++ and python version)
- Leetcode_pascals-triangle-ii (updated c++ and python version)
- RelativeLayout的理解
- Afinal简介
- iOS开发何如在调试的时候轻松找到程序在哪里崩溃
- XSLT——XML样式表转换语言
- 内核雏形——跳入保护模式
- Leetcode_sort-colors(c++ and python updated)
- 《APUE》chapter 11 Thread 学习笔记(加上自己的代码)
- 链表建立,插入,删除,输出
- http soap关系
- 黑马程序员 c语言-----循环与函数学习
- c语言两个字符串数组的连接 通过地址搞定
- cell重写layoutSubviews的问题
- 一步步学习微软InfoPath2010和SP2010--第十一章节--创建审批流程(10)--Emails的工作流
- java引用,c++指针、引用的理解