https://oj.leetcode.com/problems/plus-one/
Test instructions: An integer is stored in an int array in the order of the highest bit in array[0], the lowest bit in [n-1], for example: 98, stored as: array[0]=9; array[1]=8;
The idea of solving problems, starting with the last digit of the array and adding 1, you need to consider rounding, if there is still a carry after the [0] bit, you need to open an array of length (N.length + 1) and copy the original array.
public class Solution {public int[] PlusOne (int[] digits) { int flag = 1;int i = 0;for (i=digits.length-1;i>=0; i--) {Digits[i] = Digits[i] + flag;if (digits[i]>9) { <span style= "white-space:pre" ></span>flag = 1;< C3/><span style= "White-space:pre" ></span>digits[i] = 0; <span style= "White-space:pre" ></SPAN>} else { <span style= "White-space:pre" ></span> return digits; <span style= "White-space:pre" ></SPAN>} }if (i==-1&&flag==1) {<span style= "White-space: Pre "></span>int[] newdigits = new Int[digits.length+1];newdigits[0] = 1;for (i=1;i<=digits.length;i++) { Newdigits[i] = digits[i-1];} return newdigits;} else {return digits;}} }
Leetcode Plus One Java edition problem solving report