首页 > 其他 > 详细

LeetCode 66 Plus One

时间:2017-01-01 08:44:50      阅读:250      评论:0      收藏:0      [点我收藏+]

Problem:

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.

Analysis:

十进制数按位存储于vector,现在求给该数字加1后所得数以vector表示的形式。

Summary:

简单的大数加法。

 1 class Solution {
 2 public:
 3     vector<int> plusOne(vector<int>& digits) {
 4         int len = digits.size();
 5         int carry = 1;
 6         for (int i = len - 1; i >= 0; i--) {
 7             int sum = carry + digits[i];
 8             digits[i] = sum % 10; 
 9             carry = sum / 10;
10         }
11         
12         if (carry) {
13             digits.insert(digits.begin(), 1);
14         }
15         
16         return digits;
17     }
18 };

 

LeetCode 66 Plus One

原文:http://www.cnblogs.com/VickyWang/p/6240698.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!