Leetcode 高精度 Plus One
2014-08-29 01:58
411 查看
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
Total Accepted: 17614 Total
Submissions: 55852My 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.
题意:给定一个由数组表示大整数,数组的每一个元素对应该数的十进制表示的每一位,对该数进行加 1 操作
思路:高精度加法
复杂度:时间:O(n)
vector<int> plusOne(vector<int> &digits){
int carry = 1;
for(auto it = digits.rbegin(); it != digits.rend(); ++it){
int tmp = *it + carry;
*it = tmp % 10;
carry = tmp / 10;
if(!carry) break;
}
if(carry) digits.insert(digits.begin(), carry);
return digits;
}
Plus One
Total Accepted: 17614 TotalSubmissions: 55852My 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.
题意:给定一个由数组表示大整数,数组的每一个元素对应该数的十进制表示的每一位,对该数进行加 1 操作
思路:高精度加法
复杂度:时间:O(n)
vector<int> plusOne(vector<int> &digits){
int carry = 1;
for(auto it = digits.rbegin(); it != digits.rend(); ++it){
int tmp = *it + carry;
*it = tmp % 10;
carry = tmp / 10;
if(!carry) break;
}
if(carry) digits.insert(digits.begin(), carry);
return digits;
}
相关文章推荐
- Leetcode 高精度 Plus One
- [LeetCode] 66. Plus One
- [leetcode] plus one
- leetcode: 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.com]算法题目 - Plus One
- LeetCode 66. Plus One
- leetcode7 Plus One
- 【LeetCode】(66)Plus One (Easy)
- Leetcode-66. Plus One
- LeetCode--Plus one(加1)Python
- leetcode 33: Plus One
- [LeetCode] Plus One
- 【LeetCode】66. Plus One
- [leetcode] Plus One