66. Plus One

来源:互联网 发布:禅道 mysql数据库连接 编辑:程序博客网 时间:2024/06/05 00:20

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.



code:

<span style="font-size:14px;">public class Solution {    public int[] plusOne(int[] digits) {        int n=digits.length;        int flag=1;        int temp;        for(int i=n-1;i>=0;i--){            temp=digits[i]+flag;            flag=temp/10;            digits[i]=temp%10;        }        if(flag>0){            int[] res=new int[n+1];            res[0]=flag;            for(int i=1;i<=n;i++)                res[i]=digits[i-1];                    return res;        }        return digits;    }}</span>


0 0