leetcode-66-加一
生活随笔
收集整理的這篇文章主要介紹了
leetcode-66-加一
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
問題:
?
package com.example.demo;public class Test66 {/*** 加一:* 從后向前遍歷,分當(dāng)前位置是否為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;}}// 如果上邊錢都處理完,走到該位置時,代表原來的數(shù)組為:999格式,// 這種情況下,則需要進行重建數(shù)組,索引0處為1,其余為0int[] 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);}} }?
總結(jié)
以上是生活随笔為你收集整理的leetcode-66-加一的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: leetcode-136-只出现一次的数
- 下一篇: leetcode-49-字母异位词分组