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. ActionBar 的应用

    转载:http://blog.csdn.net/yuxlong2010 作为Android 3.0之后引入的新的对象,ActionBar可以说是一个方便快捷的导航神器.它可以作为活动的标题,突出活动的 ...

  2. iOS 清除xcode缓存和生成文件

    方法1 按快捷键 shift+command+G 或者 Finder图标点击右键选 前往文件夹... 调出前往文件夹框 在里面输入如下 /Users/(自己电脑名字)/Library/Develope ...

  3. 配置web项目session永不超时

    众所周知,当用户登录网站后较长一段时间没有与服务器进行交互,将会导致服务器上的用户会话数据(即session)被销毁.此时,当用户再次操作网页时,如果服务器进行了session校验,那么浏览器将会提醒 ...

  4. JavaScript基础-----数组(Array)

    1.JavaScript 中创建数组的方法: (1).使用Array构造函数: var arr = new Array(); //创建一个空数组 var arr = new Array(5); //传 ...

  5. AndroidMVP

    Mvp模式简介 衍生于MVC 模式,降低了耦合性,避免了View(Activity/Fragment)承担了所有的责任, 分担了UI层的职责. 在MVP模式里通常包含4个要素: * View:负责绘制 ...

  6. Swift_数组详解

    Swift_数组详解 点击查看源码 初始化 //初始化 fileprivate func testInit() { //空数组 var array = [Int]() print(array) arr ...

  7. LeetCode 翻转链表

    基本思路 从元首节点之后每次取一个节点,并将节点接到元首节点前面 代码实现 /** * Definition for singly-linked list. * struct ListNode { * ...

  8. DevOps - 项目私库 - Nexus Repository

    相关链接 Sonatype官网:https://www.sonatype.com Products: Nexus Repository OSS2.x & 3.x Documentation:  ...

  9. (九)Pycharm异常、模块

    异常:     当Python检测到一个错误时,解释器就无法继续执行了,反而出现了一些错误的提示,这就是所谓的"异常"   捕获异常:      格式:try+执行代码······ ...

  10. plsql 连接数据库无法解析指定的连接标识符

    之前用plsql连接的时候一直出问题,报无法解析指定的连接标识符,但是我加上ip地址就可以连接上. 我百度了很久,有说如下图选择oracle home的,有说清空admin目录下的所有文件, 但是都不 ...