LeetCode & Q66-Plus One-Easy

Array

Description:

Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.

You may assume the integer do not contain any leading zero, except the number 0 itself.

The digits are stored such that the most significant digit is at the head of the list.

不得不承认,我已经蠢出天际了,题目愣是没看懂,试了好几遍才看出来要干嘛(明明看得懂英文的...哎...)

这题开始有些情况没考虑到,wrong了两次....

my Solution:

public class Solution {
public int[] plusOne(int[] digits) {
int n = digits.length;
for (int i = n -1; i >= 0; i--) {
if (digits[i] != 9) {
digits[i]++;
return digits;
} else {
digits[i] = 0;
}
}
int[] array = new int[n + 1];
array[0] = 1;
return array;
}
}
上一篇:程序员是怎么炼成的---OC题集--练习答案与题目(2)


下一篇:shiro权限管理的框架-入门