leetcode | Plus One

来源:互联网 发布:电子科技集团14所 知乎 编辑:程序博客网 时间:2024/06/08 10:30

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 len = digits.length;        for(int i=len-1;i>=0;i--)        {            // System.out.println(i);            if(digits[i]==9)            {                digits[i]=0;                continue;            }            else            {                digits[i] ++;                return digits;            }        }                int[] newdigits = new int[len+1];        newdigits[0]=1;        System.arraycopy(digits,0,newdigits,1,len);        return newdigits;    }}


0 0
原创粉丝点击