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;
        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,449评论 0 10
  • 原文 我的绘制图。这做工还是很拼的,哈哈。
    画师昊渊阅读 2,246评论 8 10
  • 排序算法之堆排序 堆排序是利用堆的数据结构而设计的一种排序算法,堆排序是一种选择排序。可以利用数组的特点快速定位制...
    Ping接未来阅读 352评论 0 0
  • 在说之前,先说明protocol和delegate完全不是一回事,放在一起说,只是因为我们经常在同一个头文件里看到...
    路墨阅读 3,323评论 0 11
  • 最近有小伙伴跟我们反馈, 在工作中与老板沟通交流会存在一些困扰, 所以小编特地推送一篇如何教各位 耍诈.........
    路辰阅读 346评论 0 6