码迷,mamicode.com
首页 > 其他好文 > 详细

Plus One

时间:2018-03-03 20:28:18      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:return   lead   leading   integer   tor   --   hat   sel   array   

Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.

You may assume the integer do not contain any leading zero, except the number 0 itself.

The digits are stored such that the most significant digit is at the head of the list.

public int[] plusOne(int[] digits) {
        int length = digits.length;
        digits[length-1]+=1;
        int i = length-1;
        //做下标为1-n的位置
        while (i>0){
            if (digits[i]<10){
                return digits;
            }
            else {
                int j = digits[i]%10;
                digits[i] = j;
                digits[i-1]+=1;
            }
            i--;
        }
        //做下标为0的位置
        if (digits[0]<10){
            return digits;
        }
        else {
            digits[0]%=10;
            int[] plusone = new int[length+1];
            plusone[0]=1;
            for (int m=0;m<length;m++){
                plusone[m+1]=digits[m];
            }
            return plusone;
        }
    }

Plus One

标签:return   lead   leading   integer   tor   --   hat   sel   array   

原文地址:https://www.cnblogs.com/bingo2-here/p/8502695.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!