Given a non-empty array of digits representing a non-negative integer, plus one to the integer.

The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.

You may assume the integer does not contain any leading zero, except the number 0 itself.

Example 1:

Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.

Example 2:

Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.
 class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int s = digits.size();
int flag = ;
if(digits[] == ) {
if(digits[s - ] == ) {
for(int i = ; i < s - ; i++) {
if(digits[i] == ) {
continue;
}
else {
flag = ;
break;
}
}
if(flag == ) {
vector<int> res(s + );
res[] = ;
for(int i = ; i < s; i++)
res[i] = ;
return res;
}
}
}
if(digits[s - ] == ) {
vector<int> res(s);
res[s - ] = ;
int car = ;
for(int i = s - ; i >= ; i--) {
res[i] = digits[i] + car;
if(car == ) {
if(res[i] == )
res[i] = ;
else
car = ;
}
}
return res;
}
else {
vector<int> res(digits);
res[s - ] = digits[s - ] + ;
return res;
}
}
};

LeetCode - 66. Plus One(0ms)的更多相关文章

  1. 前端与算法 leetcode 66. 加一

    目录 # 前端与算法 leetcode 66. 加一 题目描述 概要 提示 解析 解法一 解法二 算法 # 前端与算法 leetcode 66. 加一 题目描述 给定一个由整数组成的非空数组所表示的非 ...

  2. LeetCode—66、88、118、119、121 Array(Easy)

    66. Plus One Given a non-negative integer represented as a non-empty array of digits, plus one to th ...

  3. 2017-3-7 leetcode 66 119 121

    今天纠结了一整天============================================================== leetcode66 https://leetcode.c ...

  4. leetcode 66

    66. Plus One Given a non-negative number represented as an array of digits, plus one to the number. ...

  5. LeetCode 66. Plus One(加1)

    Given a non-negative integer represented as a non-empty array of digits, plus one to the integer. Yo ...

  6. LeetCode(66)题解: Plus One

    https://leetcode.com/problems/plus-one/ 题目: Given a non-negative number represented as an array of d ...

  7. [LeetCode] 66. Plus One 加一

    Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The ...

  8. Java实现 LeetCode 66 加一

    66. 加一 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示 ...

  9. [LeetCode]66. 加一(数组)

    ###题目 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示例 ...

随机推荐

  1. Android学习笔记_36_ListView数据异步加载与AsyncTask

    一.界面布局文件: 1.加入sdcard写入和网络权限: <!-- 访问internet权限 --> <uses-permission android:name="andr ...

  2. HDFS副本存放读取

    HDFS作为Hadoop中 的一个分布式文件系统,而且是专门为它的MapReduce设计,所以HDFS除了必须满足自己作为分布式文件系统的高可靠性外,还必须为 MapReduce提供高效的读写性能,那 ...

  3. Dapper.net ORM

    参考链接:https://github.com/StackExchange/dapper-dot-net Dapper - a simple object mapper for .Net Dapper ...

  4. checkbox 全选

    <template> <div class="hello"> <table> <tr> <th><input ty ...

  5. Struts-Core jar包

    密码t6mp https://pan.baidu.com/share/init?surl=E--zExzI9-VY1zaT8F9i9w

  6. oracle远程导出/导入

    创建db_link,远程导出/导入.expdp/impdp Oracle数据库本地磁盘空间有限,或应用系统的需要,会通过远程的方式导出数据库.在oracle当中,exp远程导库的速度太慢,而expdp ...

  7. eclipse的中文插件链接及使用方法

    链接:http://www.eclipse.org/babel/downloads.php 帮助-->安装-->打开链接使用链接里面的语言包下载地址-->下载安装-->完成

  8. [国家集训队]小Z的袜子(莫队,概率)

    题目描述 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命…… 具体来说,小Z把这N只袜子从1到N编 ...

  9. Java应用:经纬度匹配(geohash加密)

    本文采用http://gc.ditu.aliyun.com地址进行经纬度匹配,无数量限制 如果给定经纬度进行geohash加密操作,先解密得到相应gps坐标,具体程序如下所示: import java ...

  10. Jupyter Notebook里面使用Matplotlib画图 图表中文乱码问题

    可查看以下链接: https://blog.csdn.net/ccblogger/article/details/79613335