Plus One

Plus One

Given a non-negative number represented as an array of digits, plus one to the number.git

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

 

public class Solution {
  public int[] plusOne(int[] digits) {
    int carry = 1;
    int temp;
    for(int i = digits.length-1; i > -1; i--){
      temp = digits[i] + carry;
      digits[i] = temp%10;
      if(temp < 10) return digits;
      carry = 1;
    }
    int[] newDigits = new int[digits.length+1];
    for(int i = newDigits.length-1; i > 0; i--)
      newDigits[i] = digits[i-1];
    newDigits[0] = 1;
    return newDigits;
  }
}io

相关文章
相关标签/搜索