首页 > 其他 > 详细

leetcode-66-加一

时间:2019-08-03 00:39:39      阅读:81      评论:0      收藏:0      [点我收藏+]

问题:

技术分享图片

 

package com.example.demo;

public class Test66 {

    /**
     * 加一:
     * 从后向前遍历,分当前位置是否为9,9时,会进一
     */
    public int[] plusOne(int[] digits) {
        for (int i = digits.length - 1; i >= 0; i--) {
            digits[i]++;
            digits[i] %= 10;
            if (digits[i] != 0) {
                return digits;
            }
        }
        // 如果上边钱都处理完,走到该位置时,代表原来的数组为:999格式,
        // 这种情况下,则需要进行重建数组,索引0处为1,其余为0
        int[] res = new int[digits.length + 1];
        res[0] = 1;
        return res;
    }

    public static void main(String[] args) {
        Test66 t = new Test66();
        int[] arr = {0};
        int[] ints = t.plusOne(arr);
        for (int anInt : ints) {
            System.out.print(anInt);
        }
    }
}

 

leetcode-66-加一

原文:https://www.cnblogs.com/nxzblogs/p/11270450.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!