首页 > 其他 > 详细

LeetCode 题解之Plus One

时间:2018-07-11 21:09:47      阅读:159      评论:0      收藏:0      [点我收藏+]

1、题目描述

技术分享图片

2、题目分析

从后向前做加法,等于10则进位,否则直接加1 ,返回 digits;

3、代码

 1 vector<int> plusOne(vector<int>& digits) {
 2         int up = 1;
 3         for( int i = digits.size()-1 ; i >= 0; i--){
 4             if( digits[i] + up < 10 ){
 5                 digits[i] += 1;
 6                 return digits;
 7             }else{
 8                 digits[i] = 0;
 9                 up = 1;
10             }
11         }
12         if( up == 1 ){
13             vector<int>::iterator it  = digits.begin() ;
14             digits.insert(it,1);
15         }
16         return digits;  
17     }

 

LeetCode 题解之Plus One

原文:https://www.cnblogs.com/wangxiaoyong/p/9296586.html

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