[LeetCode] Plus One

来源:互联网 发布:ida 复制数据 编辑:程序博客网 时间:2024/05/06 05:02

Total Accepted: 9100 Total Submissions: 29949

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 carry = 1;                for (int i = digits.length-1; i >= 0; i--) {            int temp = digits[i]+carry;            digits[i] = temp%10;            if (temp > 9 )  carry = 1;            else            carry = 0;        }                if (carry == 0) return digits;                // 100000000...        int[] tmp = new int[digits.length+1];        tmp[0] = 1;                return tmp;    }}


 

0 0
原创粉丝点击