[LeetCode]Plus One

题目描述:(链接)

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

 

posted @ 2015-10-12 17:28  skycore  阅读(97)  评论(0编辑  收藏  举报