Plus One

Posted binryang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Plus One相关的知识,希望对你有一定的参考价值。

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

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

The digits are stored such that the most significant digit is at the head of the list.

public int[] plusOne(int[] digits) {
        int length = digits.length;
        digits[length-1]+=1;
        int i = length-1;
        //做下标为1-n的位置
        while (i>0){
            if (digits[i]<10){
                return digits;
            }
            else {
                int j = digits[i]%10;
                digits[i] = j;
                digits[i-1]+=1;
            }
            i--;
        }
        //做下标为0的位置
        if (digits[0]<10){
            return digits;
        }
        else {
            digits[0]%=10;
            int[] plusone = new int[length+1];
            plusone[0]=1;
            for (int m=0;m<length;m++){
                plusone[m+1]=digits[m];
            }
            return plusone;
        }
    }

以上是关于Plus One的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode Plus One

LeetCode 题解之Plus One

Plus One

[Leetcode] Plus One

66_Plus-One

66. Plus One