Plus One

来源:互联网 发布:mac地址采集摄像机 编辑:程序博客网 时间:2024/06/07 13:02

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) {        int up=0;        int length=digits.length;        up=(digits[length-1]+1)/10;        digits[length-1]=(digits[length-1]+1)%10;        for(int i=length-2;i>=0;i--)        {            int temp=(digits[i]+up);            up=temp/10;            digits[i]=temp%10;                    }        int[] res=new int[length+1];        if(up<=0)  return digits;                            for(int i=1;i<length+1;i++)                 res[i]=digits[i-1];                        res[0]=1;        return res;    }}


0 0
原创粉丝点击