484. Find Permutation

Description

By now, you are given a secret signature consisting of character 'D' and 'I'. 'D' represents a decreasing relationship between two numbers, 'I' represents an increasing relationship between two numbers. And our secret signature was constructed by a special integer array, which contains uniquely all the different number from 1 to n (n is the length of the secret signature plus 1). For example, the secret signature "DI" can be constructed by array [2,1,3] or [3,1,2], but won't be constructed by array [3,2,4] or [2,1,3,4], which are both illegal constructing special string that can't represent the "DI" secret signature.

On the other hand, now your job is to find the lexicographically smallest permutation of [1, 2, ... n] could refer to the given secret signature in the input.

Example 1:

Input: "I"
Output: [1,2]
Explanation: [1,2] is the only legal initial spectial string can construct secret signature "I", where the number 1 and 2 construct an increasing relationship.

Example 2:

Input: "DI"
Output: [2,1,3]
Explanation: Both [2,1,3] and [3,1,2] can construct the secret signature "DI",
but since we want to find the one with the smallest lexicographical permutation, you need to output [2,1,3]

Note:

  • The input string will only contain the character 'D' and 'I'.* The length of input string is a positive integer and will not exceed 10,000

Solution

找規(guī)律茫船,time O(n), space O(1)

For example, given IDIIDD we start with sorted sequence 1234567
Then for each k continuous D starting at index i we need to reverse [i, i+k] portion of the sorted sequence.

IDIIDD
1234567 // sorted
1324765 // answer

class Solution {
    public int[] findPermutation(String s) {
        int n = s.length();
        int[] permutation = new int[n + 1];
        
        for (int i = 0; i <= n; ++i) {
            permutation[i] = i + 1;     // sorted
        }
        
        int dStart = 0;
        for (int i = 0; i < n; ++i) {
            if (s.charAt(i) != 'D') continue;
            
            int left = i;
            while (i < n && s.charAt(i) == 'D') {
                ++i;
            }
            reverse(permutation, left, i);
        }
        
        return permutation;
    }
    
    public void reverse(int[] nums, int start, int end) {
        while (start < end) {
            int tmp = nums[start];
            nums[start++] = nums[end];
            nums[end--] = tmp;
        }
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末脐雪,一起剝皮案震驚了整個(gè)濱河市拯刁,隨后出現(xiàn)的幾起案子戚长,更是在濱河造成了極大的恐慌淤井,老刑警劉巖吴裤,帶你破解...
    沈念sama閱讀 218,640評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件哨查,死亡現(xiàn)場(chǎng)離奇詭異究驴,居然都是意外死亡爷怀,警方通過(guò)查閱死者的電腦和手機(jī)阻肩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,254評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)运授,“玉大人烤惊,你說(shuō)我怎么就攤上這事∮蹼” “怎么了柒室?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,011評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)逗宜。 經(jīng)常有香客問(wèn)我雄右,道長(zhǎng),這世上最難降的妖魔是什么纺讲? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,755評(píng)論 1 294
  • 正文 為了忘掉前任擂仍,我火速辦了婚禮,結(jié)果婚禮上熬甚,老公的妹妹穿的比我還像新娘逢渔。我一直安慰自己,他們只是感情好乡括,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,774評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布肃廓。 她就那樣靜靜地躺著,像睡著了一般诲泌。 火紅的嫁衣襯著肌膚如雪亿昏。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,610評(píng)論 1 305
  • 那天档礁,我揣著相機(jī)與錄音,去河邊找鬼吝沫。 笑死呻澜,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的惨险。 我是一名探鬼主播羹幸,決...
    沈念sama閱讀 40,352評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼辫愉!你這毒婦竟也來(lái)了栅受?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,257評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎屏镊,沒(méi)想到半個(gè)月后依疼,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,717評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡而芥,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,894評(píng)論 3 336
  • 正文 我和宋清朗相戀三年律罢,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片棍丐。...
    茶點(diǎn)故事閱讀 40,021評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡误辑,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出歌逢,到底是詐尸還是另有隱情巾钉,我是刑警寧澤,帶...
    沈念sama閱讀 35,735評(píng)論 5 346
  • 正文 年R本政府宣布秘案,位于F島的核電站砰苍,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏踏烙。R本人自食惡果不足惜师骗,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,354評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望讨惩。 院中可真熱鬧辟癌,春花似錦、人聲如沸荐捻。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,936評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)处面。三九已至厂置,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間魂角,已是汗流浹背昵济。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,054評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留野揪,地道東北人访忿。 一個(gè)月前我還...
    沈念sama閱讀 48,224評(píng)論 3 371
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像斯稳,于是被迫代替她去往敵國(guó)和親海铆。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,974評(píng)論 2 355

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

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,332評(píng)論 0 10
  • 我是一棵千年古樹(shù)挣惰,在我還是一顆種子的時(shí)候卧斟,大地母親給我養(yǎng)分殴边,令我成長(zhǎng)。我的成長(zhǎng)離不開(kāi)珍语,大地母親的壞懷抱锤岸,...
    李昀哲閱讀 325評(píng)論 0 1
  • 01 提一個(gè)好的問(wèn)題是解決疑慮和提升認(rèn)知的好方法之一。 02 又忘記了思考問(wèn)題要以目標(biāo)為導(dǎo)向地思考問(wèn)題廊酣,導(dǎo)致今天分...
    小妖Troy閱讀 130評(píng)論 7 1
  • 晚上接孩子放學(xué)亡驰,孩子小心翼翼的說(shuō):“爸爸晓猛,媽媽?zhuān)銈兩塘總€(gè)事,行嗎凡辱?” “說(shuō)吧戒职,什么事?” “你們以后接我透乾,可以...
    瞎Y子閱讀 195評(píng)論 0 1