66 Plus One


title: Plus One
tags:
- plus-one
- No.66
- simple
- stack


Problem

Given a non-empty array of digits representing a non-negative integer, plus one to the integer.

The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.

You may assume the integer does not contain any leading zero, except the number 0 itself.

Example 1:

Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.

Example 2:

Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.

Corner Cases

  • empty array
  • carry digit [9,9,9,9]

Solutions

Stack

There are 2 possibilities for the length of returned array: new int[digits.length] or new int[1 + digits.length]. We can use stack as dynamic array to collect the digits and pop them to a new array.

This algorithm requires 2 loops, thus running time is 2 \times O(n). The space complexity of stack is O(n), the total cost of space is 2 \times O(n).

class Solution {
    public int[] plusOne(int[] digits) {        
        Stack<Integer> st = new Stack<Integer> ();
        
        int d = 0;
        int c = 1;
        int l = 0;
        for (int i=digits.length-1; 0<=i; i--) {
            d = digits[i] + c;
            c = (d > 9) ? 1 : 0;
            d = d - c * 10;
            st.push(d);
            l = l + 1;
        }
        if (c == 1) {
            st.push(1);
            l += 1;
        }
        
        int[] a = new int[l];
        for (int i=0; i<l; i++) {
            a[i] = st.pop();
        }
        
        return a;
    }
}

Copy Array

As we discussed before, we won't know the length of returned array until one loop is finished. But we can allocate two arrays of size digits.length and 1 + digits.length ahead instead of a stack. In the end, we return the correct length according to carry situation.

This algorithm requires 2 \times O(n) space and one-time loop O(n):

class Solution {
    public int[] plusOne(int[] digits) {
        int[] a = new int[digits.length];
        int[] b = new int[1 + digits.length];
        
        int c = 1;
        int d = 0;
        for (int i=digits.length-1; 0<=i; i--) {
            d = digits[i] + c;
            c = (d > 9) ? 1 : 0;
            d = d - c * 10;
            a[i]   = d;
            b[i+1] = d;
        }
        
        if (c == 1) {
            b[0] = 1;
            return b;
        }
        else {
            return a;
        }
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末泌射,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件耸彪,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機揽碘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來园匹,“玉大人雳刺,你說我怎么就攤上這事÷阄ィ” “怎么了掖桦?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長供汛。 經(jīng)常有香客問我枪汪,道長涌穆,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任雀久,我火速辦了婚禮宿稀,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘赖捌。我一直安慰自己原叮,他們只是感情好,可當我...
    茶點故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布巡蘸。 她就那樣靜靜地躺著奋隶,像睡著了一般。 火紅的嫁衣襯著肌膚如雪悦荒。 梳的紋絲不亂的頭發(fā)上唯欣,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天,我揣著相機與錄音搬味,去河邊找鬼境氢。 笑死,一個胖子當著我的面吹牛碰纬,可吹牛的內(nèi)容都是我干的萍聊。 我是一名探鬼主播,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼悦析,長吁一口氣:“原來是場噩夢啊……” “哼寿桨!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起强戴,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤亭螟,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后骑歹,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體预烙,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年道媚,在試婚紗的時候發(fā)現(xiàn)自己被綠了扁掸。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,841評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡最域,死狀恐怖谴分,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情羡宙,我是刑警寧澤狸剃,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站狗热,受9級特大地震影響钞馁,放射性物質(zhì)發(fā)生泄漏虑省。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一僧凰、第九天 我趴在偏房一處隱蔽的房頂上張望探颈。 院中可真熱鬧,春花似錦训措、人聲如沸伪节。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽怀大。三九已至,卻和暖如春呀闻,著一層夾襖步出監(jiān)牢的瞬間化借,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工捡多, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留蓖康,地道東北人。 一個月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓垒手,卻偏偏與公主長得像蒜焊,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子科贬,可洞房花燭夜當晚...
    茶點故事閱讀 44,781評論 2 354

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

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,325評論 0 10
  • 原文 我的繪制圖泳梆。這做工還是很拼的,哈哈唆迁。
    畫師昊淵閱讀 2,199評論 8 10
  • 排序算法之堆排序 堆排序是利用堆的數(shù)據(jù)結(jié)構(gòu)而設(shè)計的一種排序算法鸭丛,堆排序是一種選擇排序√圃穑可以利用數(shù)組的特點快速定位制...
    Ping接未來閱讀 342評論 0 0
  • 在說之前,先說明protocol和delegate完全不是一回事瘾带,放在一起說鼠哥,只是因為我們經(jīng)常在同一個頭文件里看到...
    路墨閱讀 3,277評論 0 11
  • 最近有小伙伴跟我們反饋朴恳, 在工作中與老板溝通交流會存在一些困擾, 所以小編特地推送一篇如何教各位 耍詐.........
    路辰閱讀 330評論 0 6