LeetCode 066 Plus One

题目描述

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

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

代码

public class Solution {

    public int[] plusOne(int[] digits) {

        if (digits == null || digits.length == 0) {
            return null;
        }

        int[] reslut = new int[digits.length + 1];
        digits[digits.length - 1]++;

        for (int i = digits.length - 1; i >= 0; i--) {
            reslut[i + 1] += digits[i];
            reslut[i] += reslut[i + 1] / 10;
            reslut[i + 1] %= 10;
        }

        if (reslut[0] == 0) {
            return Arrays.copyOfRange(reslut, 1, reslut.length);
        } else {
            return reslut;
        }
    }
}

参考链接

LeetCode 066 Plus One

    原文作者:_我们的存在
    原文地址: https://blog.csdn.net/Yano_nankai/article/details/48876763
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞