日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

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

leetcode || 75、Sort Colors

來源:程序員人生   發布時間:2015-04-27 08:36:44 閱讀次數:3288次

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組成的數組按升序排序,不能使用字典排序

thinking:

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

(2)最優算法是 快速排序的變形: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++; } } };


生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 国产成人久久 | 亚洲一区二区三区四区五区中文 | 免费日韩电影 | 1000部精品久久久久久久久 | 一区二区在线视频 | 性视频在线播放 | 视频一区在线播放 | 成人国产精品免费网站 | 热久久久 | 美女久久久 | 国产成人精品av | 玖玖在线免费视频 | 亚洲免费三级 | 人人澡人人爽 | 亚洲一区在线播放 | 日韩综合精品 | 久久久久久久久网站 | 一区二区三区观看 | 成人伊人网 | 成人国产精品免费观看视频 | 国产欧美一区二区三区在线看 | 国产精品久久久久久久久久三级 | 91精品国产欧美一区二区成人 | 亚洲成人高清 | 国产一区二区三区四区www. | 欧美日韩免费在线观看 | 国产一级免费 | 欧美三级一区 | 亚洲精品一二三区 | 免费观看成人 | 亚洲一二三在线 | 日韩在线一区二区三区 | 中文字幕一区二区三区中文字幕 | 成人免费视频网 | 成人av在线网站 | 欧美日韩国产三区 | 国产成在线观看免费视频 | 三级免费电影 | 羞羞视频免费网站 | 久久久成人精品 | 国产精品一区久久久 |