多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當前位置:首頁 > php框架 > 框架設計 > leetcode || 75、Sort Colors

leetcode || 75、Sort Colors

來源:程序員人生   發(fā)布時間:2015-04-27 08:36:44 閱讀次數(shù):3281次

problem:

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?

Hide Tags
 Array Two Pointers Sort
題意:將由0、1、2組成的數(shù)組按升序排序,不能使用字典排序

thinking:

(1)最簡單的是使用計數(shù)法,計算0、1、2 的個數(shù),最后再填充

(2)最優(yōu)算法是 快速排序的變形:1次遍歷完成排序

code:

class Solution { public: void sortColors(int A[], int n) { int i = 0; // 0 pointer int j = n - 1; // 1 pointer int k = n - 1; // 2 pointer while (i <= j) { if (A[i] == 2) { int t = A[k]; A[k] = A[i]; A[i] = t; k--; if (k < j) j = k; } else if (A[i] == 1) { int t = A[j]; A[j] = A[i]; A[i] = t; j--; } else i++; } } };


生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 欧美午夜性春猛xxxx | 亚洲性另类 | 99中文在线| 一级欧美一级日韩片 | 图片区小说区号综合区 | 欧美一二 | 国产欧美日韩综合一区二区三区 | 亚洲欧美日韩综合一区 | 最近中文字幕视频 | 午夜在线亚洲 | 欧美日韩加勒比一区二区三区 | 色阁在线 | 日本特黄视频 | 12306播播影视播播影院午夜 | 亚洲精品视频久久久 | 激情97| 91亚洲影院| 亚洲国产欧美国产第一区二区三区 | 亚洲欧美日韩第一页 | xxxwww日本高清 | 成人乱码一区二区三区四区 | 日韩精品福利在线 | 动漫精品欧美一区二区三区 | 韩日一区二区 | 久草午夜| tube 欧美巨大tvhd | 69成人免费视频 | 亚洲国产精品aaa一区 | 一二三四在线观看免费播放视频 | 欧美一级久久久久久久大片动画 | 免看一级a毛片一片成人不卡 | 国产成人久久精品激情91 | 欧美偷拍自拍视频 | 久久91av | 色综合一本到久久亚洲91 | 亚洲欧美日韩久久精品第一区 | 日本啊啊视频 | 国产在线精品福利91香蕉 | 最新国产精品亚洲二区 | 欧美一区二区三区不卡免费 | 日本xxxwww免费视频 |