您的位置:首页 > 其它

【LeetCode】 066. Plus One

2016-08-02 11:32 330 查看
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) {
boolean flag = false;
digits[digits.length - 1]++;
for (int i = digits.length - 1; i >= 0; i--) {
digits[i] += flag ? 1 : 0;
if (digits[i] > 9) {
flag = true;
digits[i] -= 10;
} else {
flag = false;
break;
}
}
if (flag) {
int[] res = new int[digits.length + 1];
System.arraycopy(digits, 0, res, 1, digits.length);
res[0] = 1;
return res;
}
return digits;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: