75. Sort Colors

Given an array withnobjects 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.
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?

red 指針在前, blue 指針在后, i 從頭掃描直到遇到blue指針

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,037評(píng)論 0 23
  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問(wèn)題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,899評(píng)論 0 33
  • 自己的事情自己承擔(dān),沒人可以替你分擔(dān)。自己的路還得自己走。自己的路還得自己填。所有的事情都會(huì)好好的走下去的。原來(lái)他...
    蓮花舒梓慧閱讀 45評(píng)論 0 0
  • 今天居然一口氣把《人生》整部小說(shuō)看完了,路遙的成名之作,名不虛傳。 主人公高加林本是村子里的民辦學(xué)校的老師,因?yàn)榇?..
    d601ea2d8638閱讀 759評(píng)論 0 0
  • 一早去山里越野跑現(xiàn)場(chǎng)擺攤,選手們出發(fā)后覺得好無(wú)聊呀,于是就想去走一個(gè)10公里,大概兩小時(shí)回來(lái),九點(diǎn)就出發(fā)了,可是下...
    112233D閱讀 373評(píng)論 0 0

友情鏈接更多精彩內(nèi)容