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;
    }
}
posted @ 2017-07-13 11:18  6002  阅读(98)  评论(0编辑  收藏  举报