您的位置:首页 > 编程语言 > Go语言

[LeetCode] Plus One

2014-04-08 16:17 357 查看
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;
}
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息