Leetcode 高精度 Plus One
來源:程序員人生 發布時間:2014-09-17 11:14:32 閱讀次數:2140次
本文為senlie原創,轉載請保留此地址:http://blog.csdn.net/zhengsenlie
Plus One
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;
}
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈