您的位置:首页 > 其它

Leetcode 66. Plus One

2016-12-21 12:10 369 查看
public class Solution {
public boolean isCarry(int num) {
return (num/10 == 1) ? true : false;
}

public int[] plusOne(int[] digits) {
int i = digits.length-1;
while (i>-1) {
if (!isCarry(++digits[i])) return digits;
else digits[i--] = 0;
}
// if previous codes don't return, it must be the case e.g. 9999+1
// therefore, create an array and set the value as 10000
// deafault value is 0, just need to set the first value as 1
int[] new_dig = new int[digits.length+1];
new_dig[0] = 1;
return new_dig;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: