LeetCode--Plus One

来源:互联网 发布:男士发型设计软件 编辑:程序博客网 时间:2024/05/03 03:51

这个题目与java里的BigInteger实现有些类似,可以参考其源码。

题目:

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) {        for(int i=digits.length-1;i>=0;i--){            int temp=digits[i]+1;            if(temp>=10){                digits[i]=temp%10;            }else{                digits[i]=temp;                break;            }        }        if(digits[0]==0){            int[] re=new int[digits.length+1];            re[0]=1;            for(int j=1;j<re.length;j++){                re[j]=digits[j-1];            }            return re;        }        return digits;    }}



0 0
原创粉丝点击