66. Plus One

来源:互联网 发布:linux 命令大全文库 编辑:程序博客网 时间:2024/05/29 11:56

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.

Subscribe to see which companies asked this question

其实还是写的有点多 while就行了

public class Solution {    public int[] plusOne(int[] digits) {        int len = digits.length;        int add = 1;        for(int i = len-1;i>=0;i--){            digits[i]=digits[i]+add;            if(digits[i]>=10){                digits[i]-=10;                add=1;            }else{                add=0;            }        }        if(add==1){            int [] ret =new int [len+1];            ret[0]=1;            return ret;        }else{            return digits;        }    }}

0 0
原创粉丝点击