LeetCode (29)-Plus One
Question:
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.
Idea: use an array to represent a non-negative integer in the range of 0 ~ The number of 9, simulate an addition plus operation to determine whether to carry. If it is 9, add 1 to 10, set it to 0, and then carry. If the limit is exceeded, re-set an array. The first is 1, followed by 0-code:
Public class Solution {public int [] plusOne (int [] digits) {int carries = 1; for (int I = digits. length-1; I> = 0 & carries> 0; I --) {// fast break when carries equals zero int sum = digits [I] + 1; 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] = 0 ;}return rst ;}}