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.
一个数使用数组表示每一位,求加一之后的数,同样返回数组。关键是加一之后变成10.public int[] plusOne(int[] digits) { // Start typing your Java solution below // DO NOT write main() function int len = digits.length - 1; int flag = 1; do{ int temp = digits[len] + flag; if(temp == 10){ digits[len] = 0; flag = 1; }else{ digits[len] = temp; flag = 0; } len--; }while(flag == 1 && len >= 0); if(flag == 0){ return digits; }else{ int[] dig = new int[digits.length + 1]; dig[0] = 1; for(int i = 0; i < digits.length; i ++){ dig[i + 1] = 0; } return dig; } }
原文地址:http://blog.csdn.net/u010378705/article/details/29656863