Plus One

来源:互联网 发布:matlab 2016b mac下载 编辑:程序博客网 时间:2024/06/07 09:42

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.

意思其实就是这个数组代表着一个整数,然后这个数+1,再用数组来表示。

public int[] plusOne(int[] digits) {        int len = digits.length;        for(int i = len-1;i>=0;i--)        {        if(digits[i]!=9)        {        digits[i]++;        return digits;//直接就可以返回             }        else        {        digits[i]=0;//若为9,则再继续看下位,该位置0        }        }        int[] newint = new int[len+1];        newint[0] = 1;        return newint;    }


0 0