首页 > 其他 > 详细

LeetCode - Refresh - Sort Colors

时间:2015-03-23 15:47:35      阅读:233      评论:0      收藏:0      [点我收藏+]
 1 class Solution {
 2 public:
 3     void sortColors(int A[], int n) {
 4         auto swap = [](int &a, int &b){int t = a; a = b; b = t;};
 5         int start = 0, end = n-1, runner = 0;
 6         while (runner <= end) {
 7             if (A[runner] == 0) {
 8                 swap(A[runner++], A[start++]);
 9                 continue;
10             }
11             if (A[runner] == 2) {
12                 swap(A[runner], A[end--]);
13                 continue;
14             }
15             runner++;
16         }
17     }
18 };

 

LeetCode - Refresh - Sort Colors

原文:http://www.cnblogs.com/shuashuashua/p/4359863.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!