leetcode-66-加一
Posted nxzblogs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode-66-加一相关的知识,希望对你有一定的参考价值。
问题:
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-加一的主要内容,如果未能解决你的问题,请参考以下文章