Leetcode 66 Plus One

来源:互联网 发布:软件工程和软件危机 编辑:程序博客网 时间:2024/06/05 08:44

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.

这个题就是一个进位的问题 没什么好多说的 唯一要提醒自己注意的是 当结果多出来一位的时候 原来的数组是不够的

于是要new 一个新的数组来返回结果

public class Solution {    public int[] plusOne(int[] digits) {        int n = digits.length;        digits[n-1]++;        for(int i = n-1; i>=0;i--){            if(digits[i]==10&&i!=0){            digits[i-1]++;            digits[i]=0;                }            if(digits[i]==10 && i==0){                               int[] result = new int[n + 1];            for(int j=n-1;j>=0;j--){                result[j+1]=digits[j];            }              result[0]=1;            result[1]=0;            return result;            }        }        return digits;    }}


0 0