LeetCode--No.66--Plus One

    xiaoxiao2025-02-08  8

    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.

    Subscribe to see which companies asked this question

    思路:模拟进位。从最后一位加加加。

    public class Solution { public int[] plusOne(int[] digits) { int length = digits.length; for(int i = length-1; i >= 1; i--){ if (digits[i] < 9){ digits[i]++; return digits; } else{ digits[i] = 0; } } if(digits[0] < 9){ digits[0]++; return digits; } else{ int[] res = new int[length+1]; res[0] = 1; return res; } } }

    转载请注明原文地址: https://ju.6miu.com/read-1296229.html
    最新回复(0)