2021-12-18_学习算法problem407“加一”问题
学习算法problem407:“加一”问题
public class Solution { public int[] plusOne(int[] digits) { int carries = 1; for (int i = digits.length - 1; i >= 0 && carries > 0; i--) { // 当没有进位时,退出循环 int sum = digits[i] + carries; digits[i] = sum % 10; carries = sum / 10; } if (carries == 0) { return digits; } int[] rst = new int[digits.length + 1]; rst[0] = 1; for (int i = 1; i < rst.length; i++) { rst[i] = digits[i - 1]; } return rst; } }