码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode(66)题解: Plus One

时间:2015-08-06 22:00:17      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

https://leetcode.com/problems/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位,逢9进1。注意999这种情况要首位insert1。

 1 class Solution {
 2 public:
 3     vector<int> plusOne(vector<int>& digits) {
 4         int n=digits.size();
 5         bool jinyi=false;
 6         for(int i=n-1;i>=0;i--){
 7             if(digits[i]==9){
 8                 digits[i]=0;
 9                 jinyi=true;
10             }
11             else{
12                 digits[i]++;
13                 jinyi=false;
14                 break;
15             }
16         }
17         if(jinyi==true){
18             vector <int>::iterator theIterator = digits.begin(); 
19             digits.insert(theIterator,1,1);
20         }
21         return digits;
22     }
23 };

 

LeetCode(66)题解: Plus One

标签:

原文地址:http://www.cnblogs.com/aezero/p/4709203.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!