您的位置:首页 > 其它

LeetCode-Plus One

2014-09-13 21:10 197 查看
作者:disappearedgod
文章出处:/article/3730225.html
时间:2014-9-13

题目



Plus One

Total Accepted: 19009 Total
Submissions: 60196My Submissions

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.

Have you been asked this question in an interview?

想法

代码

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


结果



My Submissions for Plus One

Submit TimeStatusRun TimeLanguage
3 minutes agoAccepted416 msjava
3 minutes agoAccepted408 msjava

返回

LeetCode
Solution(持续更新,java>c++)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: