leetcode-66. Plus One

来源:互联网 发布:java学徒0基础骗局 编辑:程序博客网 时间:2024/06/05 07:11

leetcode-66. Plus One

题目:

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.

需要考虑进位

public class Solution {    public int[] plusOne(int[] digits) {        if(digits.length<1) return digits;        int count  = 1,i=digits.length-1;        while(i>=0){            digits[i] += count;            count = digits[i]/10;            digits[i] = digits[i]%10;            i--;        }        if(count ==1){            int[] ret = new int[digits.length+1];            for(int j = 1; j< ret.length ;j++){                ret[j] = digits[j-1];            }            ret[0] = 1;            return ret;        }        return digits;    }}
0 0