美文网首页
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;
        }
    }
}

相关文章

  • LeetCode 66-70

    66. Plus One[https://leetcode-cn.com/problems/plus-one/] ...

  • 66. Plus One

    66. Plus One 题目:https://leetcode.com/problems/plus-one/ 难...

  • 66 Plus One

    title: Plus Onetags:- plus-one- No.66- simple- stack Prob...

  • Google面经(持续更新)

    66. Plus One Given a non-negative integer represented as ...

  • leetcode:66. Plus One

    66. Plus One Description Given a non-empty array of digit...

  • LeetCode算法题-Easy-Math(66)

    66、Plus One 题目:Given a non-empty array of digits represen...

  • 66. Plus One

    Problem Given a non-empty array of digits representing a ...

  • 66. Plus One

    https://leetcode.com/problems/plus-one/description/解题思路:本...

  • 66. Plus One

    题目分析 原题链接,登陆 LeetCode 后可用直接用一个例子来说明本题的含义,假设给定一个数字 13999,那...

  • 66. Plus One

    QuestionGiven a non-negative integer represented as a non...

网友评论

      本文标题:66 Plus One

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