LeetCode 066 Plus One

来源:互联网 发布:常州骏飞网络 编辑:程序博客网 时间:2024/05/16 12:58

题目描述

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

2 0
原创粉丝点击