美文网首页
66 Plus One

66 Plus One

作者: yangminz | 来源:发表于2018-07-16 00:09 被阅读8次

    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;
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:66 Plus One

          本文链接:https://www.haomeiwen.com/subject/ojvmpftx.html