leetcode--Plus One
2017-08-08 09:38
357 查看
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.
题意:给定一个数组,元素都是非负数,整个数组用于表示一个整数。现在这个整数加1,求加1后的数组。
分类:数组,数学
解法1:从后往前加,注意保留flag来表示进位。
[java] view
plain copy
public class Solution {
public int[] plusOne(int[] digits) {
int len = digits.length;
int[] res = new int[len+1];
int flag = 1;
for(int i=len-1;i>=0;i--){
int t = digits[i]+flag;
flag = t/10;
digits[i] = t%10;
res[i] = t%10;
}
if(flag==1){
res[0] = 1;
return res;
}
return digits;
}
}
原文链接http://blog.csdn.net/crazy__chen/article/details/46008945
The digits are stored such that the most significant digit is at the head of the list.
题意:给定一个数组,元素都是非负数,整个数组用于表示一个整数。现在这个整数加1,求加1后的数组。
分类:数组,数学
解法1:从后往前加,注意保留flag来表示进位。
[java] view
plain copy
public class Solution {
public int[] plusOne(int[] digits) {
int len = digits.length;
int[] res = new int[len+1];
int flag = 1;
for(int i=len-1;i>=0;i--){
int t = digits[i]+flag;
flag = t/10;
digits[i] = t%10;
res[i] = t%10;
}
if(flag==1){
res[0] = 1;
return res;
}
return digits;
}
}
原文链接http://blog.csdn.net/crazy__chen/article/details/46008945
相关文章推荐
- LeetCode: 66. Plus One
- Leetcode 高精度 Plus One
- Leetcode:66. Plus One
- [LeetCode]Plus One
- leetCode 66. Plus One 数组
- leetcode题解-66. Plus One
- Leetcode 66. Plus One
- LeetCode (14) Plus One
- Leetcode[66]-Plus One
- 2018.3.12 leetcode打卡 #66 plus one
- LeetCode之“数学”:Plus One
- LeetCode 66.Plus One
- 【Leetcode】Plus One
- leetcode--Plus One
- 【LeetCode-面试算法经典-Java实现】【066-Plus One(加一)】
- 《leetCode》:Plus One
- LeetCode:Plus One
- leetcode 66.Plus One
- LeetCode: Plus One
- leetcode-66. Plus One