681. Next Closest Time

Given a time represented in the format "HH:MM", form the next closest time by reusing the current digits. There is no limit on how many times a digit can be reused.

You may assume the given input string is always valid. For example, "01:34", "12:09" are all valid. "1:34", "12:9" are all invalid.

Example 1:

Input: "19:34"
Output: "19:39"

Explanation: The next closest time choosing from digits 1, 9, 3, 4, is 19:39, which occurs 5 minutes later. It is not 19:33, because this occurs 23 hours and 59 minutes later.

Example 2:

Input: "23:59"
Output: "22:22"

Explanation: The next closest time choosing from digits 2, 3, 5, 9, is 22:22. It may be assumed that the returned time is next day's time since it is smaller than the input time numerically.

一刷
題解:
對于"HH:M_", "HH:M", "H:MM", "_H:MM", apply 尋找下一個(gè)最大的digit.

  1. 首先對所有的digit排序 "17:38" -> digits[] {'1', '3', '7', '8'}
  2. 對于"HH:M_",如果已經(jīng)是最大一位digits[3], 比如8,那么下一個(gè)就為digits[0]
  3. 對于"HH:M_", upperLimit是5(00-59), 那么我們就在(digits[2], 6)中尋找朝巫,如果找不到,那么就為digits[0]
  4. 對于"H_:MM", 如果input[0] == 2, 那么upperlimit為2瞒斩,否則為9
  5. 對于"_H:MM", upperlimit是2

findnext函數(shù)的思路是冯袍,我們在當(dāng)前的digit和upperlimit中尋找第一個(gè)大于當(dāng)前的digit,如果找不到稠氮,就選digit[0]

class Solution {
    
    public String nextClosestTime(String time) {
        char[] result = time.toCharArray();
        char[] digits = new char[] {result[0], result[1], result[3], result[4]};
        Arrays.sort(digits);
        
        // find next digit for HH:M_
        result[4] = findNext(result[4], (char)('9' + 1), digits);  // no upperLimit for this digit, i.e. 0-9
        if(result[4] > time.charAt(4)) return String.valueOf(result);  // e.g. 23:43 -> 23:44
        
        // find next digit for HH:_M
        result[3] = findNext(result[3], '5', digits);
        if(result[3] > time.charAt(3)) return String.valueOf(result);  // e.g. 14:29 -> 14:41
        
        // find next digit for H_:MM
        result[1] = result[0] == '2' ? findNext(result[1], '3', digits) : findNext(result[1], (char)('9' + 1), digits);
        if(result[1] > time.charAt(1)) return String.valueOf(result);  // e.g. 02:37 -> 03:00 
        
        // find next digit for _H:MM
        result[0] = findNext(result[0], '2', digits);
        return String.valueOf(result);  // e.g. 19:59 -> 11:11
    }
    
    /** 
     * find the next bigger digit which is no more than upperLimit. 
     * If no such digit exists in digits[], return the minimum one i.e. digits[0]
     * @param current the current digit
     * @param upperLimit the maximum possible value for current digit
     * @param digits[] the sorted digits array
     * @return 
     */
    private char findNext(char current, char upperLimit, char[] digits) {
        //System.out.println(current);
        if(current == upperLimit) {
            return digits[0];
        }
        int pos = Arrays.binarySearch(digits, current) + 1;
        while(pos < 4 && (digits[pos] > upperLimit || digits[pos] == current)) { // traverse one by one to find next greater digit
            pos++;
        }
        return pos == 4 ? digits[0] : digits[pos];
    }
    
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市半开,隨后出現(xiàn)的幾起案子隔披,更是在濱河造成了極大的恐慌,老刑警劉巖寂拆,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件奢米,死亡現(xiàn)場離奇詭異,居然都是意外死亡纠永,警方通過查閱死者的電腦和手機(jī)鬓长,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來尝江,“玉大人涉波,你說我怎么就攤上這事∶埃” “怎么了怠蹂?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長少态。 經(jīng)常有香客問我城侧,道長,這世上最難降的妖魔是什么彼妻? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任嫌佑,我火速辦了婚禮,結(jié)果婚禮上侨歉,老公的妹妹穿的比我還像新娘屋摇。我一直安慰自己,他們只是感情好幽邓,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布炮温。 她就那樣靜靜地躺著,像睡著了一般牵舵。 火紅的嫁衣襯著肌膚如雪柒啤。 梳的紋絲不亂的頭發(fā)上倦挂,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天,我揣著相機(jī)與錄音担巩,去河邊找鬼方援。 笑死,一個(gè)胖子當(dāng)著我的面吹牛涛癌,可吹牛的內(nèi)容都是我干的犯戏。 我是一名探鬼主播,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼拳话,長吁一口氣:“原來是場噩夢啊……” “哼先匪!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起假颇,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤胚鸯,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后笨鸡,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡坦冠,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年形耗,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片辙浑。...
    茶點(diǎn)故事閱讀 40,102評論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡激涤,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出判呕,到底是詐尸還是另有隱情倦踢,我是刑警寧澤,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布侠草,位于F島的核電站辱挥,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏边涕。R本人自食惡果不足惜晤碘,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望功蜓。 院中可真熱鬧园爷,春花似錦、人聲如沸式撼。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽著隆。三九已至扰楼,卻和暖如春甘改,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背灭抑。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工十艾, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人腾节。 一個(gè)月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓忘嫉,卻偏偏與公主長得像,于是被迫代替她去往敵國和親案腺。 傳聞我的和親對象是個(gè)殘疾皇子庆冕,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,044評論 2 355