算法題目-19周-Reveal Cards In Increasing Order

In a deck of cards, every card has a unique integer. You can order the deck in any order you want.

Initially, all the cards start face down (unrevealed) in one deck.
Now, you do the following steps repeatedly, until all cards are revealed:

1、Take the top card of the deck, reveal it, and take it out of the deck.
2、If there are still cards in the deck, put the next top card of the deck at the bottom of the deck.
3哼审、If there are still unrevealed cards, go back to step 1. Otherwise, stop.
Return an ordering of the deck that would reveal the cards in increasing order.

The first entry in the answer is considered to be the top of the deck.

Example 1:

Input: [17,13,11,2,3,5,7]
Output: [2,13,3,11,5,17,7]
Explanation: 
We get the deck in the order [17,13,11,2,3,5,7] (this order doesn't matter), and reorder it.
After reordering, the deck starts as [2,13,3,11,5,17,7], where 2 is the top of the deck.
We reveal 2, and move 13 to the bottom.  The deck is now [3,11,5,17,7,13].
We reveal 3, and move 11 to the bottom.  The deck is now [5,17,7,13,11].
We reveal 5, and move 17 to the bottom.  The deck is now [7,13,11,17].
We reveal 7, and move 13 to the bottom.  The deck is now [11,17,13].
We reveal 11, and move 17 to the bottom.  The deck is now [13,17].
We reveal 13, and move 17 to the bottom.  The deck is now [17].
We reveal 17.
Since all the cards revealed are in increasing order, the answer is correct.

Note:

1魔吐、1 <= A.length <= 1000
2、1 <= A[i] <= 10^6
3指厌、A[i] != A[j] for all i != j

自已版本一,暴力解法,模擬過程

class Solution {
    public int[] deckRevealedIncreasing(int[] deck) {
        int[] ret = new int[deck.length];
        Arrays.sort(deck);
        List<Integer> local = new LinkedList();
        for(int i=0;i<deck.length;i++){
            local.add(i,i);
        }

        int num = 0;
        while(!local.isEmpty()){
            ret[local.get(0)] = deck[num];
            local.remove(0);
            num++;
            if(!local.isEmpty()){
                local.add(local.get(0));
                local.remove(0);
            }
        }
        
        return ret;

    }
}

Runtime: 3 ms, faster than 34.83% of Java online submissions for Reveal Cards In Increasing Order.
Memory Usage: 39.1 MB, less than 14.29% of Java online submissions for Reveal Cards In Increasing Order.

自已版本二纲爸,優(yōu)化一下不用鏈表,用數(shù)組

class Solution {
    public int[] deckRevealedIncreasing(int[] deck) {
        int[] ret = new int[deck.length];
        Arrays.sort(deck);
        int[] local = new int[deck.length*2];
        for(int i=0;i<deck.length;i++){
            local[i]=i;
        }

        int num = 0;
        int n = 0;
        while(true){

            ret[local[num]] = deck[n];
            local[n+deck.length]=local[num+1];
            num = num + 2;
            n++;
            if(n>=deck.length){
                break;
            }
        }

        return ret;

    }
} 

Runtime: 1 ms, faster than 100.00% of Java online submissions for Reveal Cards In Increasing Order.
Memory Usage: 36.4 MB, less than 100.00% of Java online submissions for Reveal Cards In Increasing Order.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末妆够,一起剝皮案震驚了整個濱河市识啦,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌责静,老刑警劉巖袁滥,帶你破解...
    沈念sama閱讀 212,454評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異灾螃,居然都是意外死亡题翻,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,553評論 3 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來嵌赠,“玉大人塑荒,你說我怎么就攤上這事〗Γ” “怎么了齿税?”我有些...
    開封第一講書人閱讀 157,921評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長炊豪。 經(jīng)常有香客問我凌箕,道長,這世上最難降的妖魔是什么词渤? 我笑而不...
    開封第一講書人閱讀 56,648評論 1 284
  • 正文 為了忘掉前任牵舱,我火速辦了婚禮,結(jié)果婚禮上缺虐,老公的妹妹穿的比我還像新娘芜壁。我一直安慰自己,他們只是感情好高氮,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,770評論 6 386
  • 文/花漫 我一把揭開白布慧妄。 她就那樣靜靜地躺著,像睡著了一般剪芍。 火紅的嫁衣襯著肌膚如雪塞淹。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,950評論 1 291
  • 那天紊浩,我揣著相機(jī)與錄音窖铡,去河邊找鬼。 笑死坊谁,一個胖子當(dāng)著我的面吹牛费彼,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播口芍,決...
    沈念sama閱讀 39,090評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼箍铲,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了鬓椭?” 一聲冷哼從身側(cè)響起颠猴,我...
    開封第一講書人閱讀 37,817評論 0 268
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎小染,沒想到半個月后翘瓮,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,275評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡裤翩,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,592評論 2 327
  • 正文 我和宋清朗相戀三年资盅,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,724評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡呵扛,死狀恐怖每庆,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情今穿,我是刑警寧澤缤灵,帶...
    沈念sama閱讀 34,409評論 4 333
  • 正文 年R本政府宣布,位于F島的核電站蓝晒,受9級特大地震影響腮出,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜芝薇,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,052評論 3 316
  • 文/蒙蒙 一利诺、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧剩燥,春花似錦、人聲如沸立倍。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,815評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽口注。三九已至变擒,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間寝志,已是汗流浹背娇斑。 一陣腳步聲響...
    開封第一講書人閱讀 32,043評論 1 266
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留材部,地道東北人毫缆。 一個月前我還...
    沈念sama閱讀 46,503評論 2 361
  • 正文 我出身青樓,卻偏偏與公主長得像乐导,于是被迫代替她去往敵國和親苦丁。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,627評論 2 350

推薦閱讀更多精彩內(nèi)容