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学习笔记_8_使用SharedPreferences存储数据

    1.SharedPreferences介绍: Android平台给我们提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数.使用SharedPrefer ...

  2. Ueditor上传图片到本地改造到上传图片到七牛云存储

    作为新手说多了都是泪啊!我特别想记录一下作为菜鸟时的坑.看看以后是否会看着笑出来. 为什么要改到云存储上就不说了.好处多多. 视频教程上使用的又拍云同时也提到了七牛云.下来我自己也查了下.又拍云是试用 ...

  3. Vue nodejs商城项目-商品列表页面组件

    data(){        return {            goodsList:[], // 商品列表            priceFilter:[ // 价格区间数组          ...

  4. Swift_Set详解

    Swift_Set详解 点击查看源码 初始化 //初始化 fileprivate func testInit() { //空Set var set = Set<String>() prin ...

  5. 魔板 Magic Squares(广搜,状态转化)

    题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 我们知道魔板的每一个方格都有一种颜色.这8种颜 ...

  6. P1247 取火柴游戏

    题目描述 输入k及k个整数n1,n2,-,nk,表示有k堆火柴棒,第i堆火柴棒的根数为ni:接着便是你和计算机取火柴棒的对弈游戏.取的规则如下:每次可以从一堆中取走若干根火柴,也可以一堆全部取走,但不 ...

  7. Java分享笔记:Java网络编程--TCP程序设计

    [1] TCP编程的主要步骤 客户端(client): 1.创建Socket对象,构造方法的形参列表中需要InetAddress类对象和int型值,用来指明对方的IP地址和端口号. 2.通过Socke ...

  8. Flask模拟实现CSRF攻击

    CSRF CSRF全拼为Cross Site Request Forgery,译为跨站请求伪造. CSRF指攻击者盗用了你的身份,以你的名义发送恶意请求. 包括:以你名义发送邮件,发消息,盗取你的账号 ...

  9. 重新格式化hadoop的namenode导致datanode无法启动的最简单解决办法

    一般namenode只格式化一次,重新格式化不仅会导致之前的数据都不可用,而且datanode也会无法启动.在datanode日志中会有类似如下的报错信息: java.io.IOException: ...

  10. simpleXML技术解析xml文件(php)

    1.simpleXML的核心思想:以面向对象的方法来操作xml文件 此技术可以将xml文件的所有元素都转成对象.会返回一个对象数组,再用foreach遍历,即可得到元素的名称,内容,和属性值. tes ...