Leetcode题解 66. Plus One

来源:互联网 发布:淘宝商城cf道具 编辑:程序博客网 时间:2024/05/10 21:57

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 static int[] plusOne(int[] digits) {        int sub=0;        if(digits.length==1){            digits[0]+=1;            if(digits[0]>9){                int[] temp=new int[2];                temp[0]=1;                temp[1]=digits[0]-10;                return temp;            }        }        for(int i=digits.length-1;i>0;i--){            if(sub==1||i==digits.length-1)              digits[i]+=1;            if(digits[i]>9){                sub=1;                digits[i]-=10;            }else{                sub=0;            }        }        if(sub==1) {            digits[0]+=1;            if(digits[0]>9){                int[] temp=new int[digits.length+1];                temp[0]=1;                digits[0]-=10;                for(int i=1;i<temp.length;i++){                    temp[i]=digits[i-1];                }                return temp;            }        }        return digits;    }}
0 0
原创粉丝点击