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. ------------------------------------------------------------------------------------------------
这个题关键是如何进位以及判断数组的首位前是否进1。可以用一个辅助标记来帮助我们做到这些。 C++代码:
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int carry = ;
int len = digits.size();
for(int i = len - ; i >= ; i--){
int a = digits[i];
if(i == len - ){
int sum = a + carry + ;
digits[i] = sum % ;
carry = sum / ; //进一位。
}
else{
int sum = a + carry;
digits[i] = sum % ;
carry = sum / ;
}
}
if(carry != ){ //表明前面还得进位。
digits.insert(digits.begin(),carry);
}
return digits;
}
};
												

(数组) leetcode 66. Plus One的更多相关文章

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

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

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

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

  3. LeetCode初级算法之数组:66 加一

    加一 题目地址:https://leetcode-cn.com/problems/plus-one/ 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一.最高位数字存放在数组的首位, 数 ...

  4. 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 ...

  5. 2017-3-7 leetcode 66 119 121

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

  6. leetcode 66

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

  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. Java [Leetcode 66]Plus One

    题目描述: Given a non-negative number represented as an array of digits, plus one to the number. The dig ...

随机推荐

  1. deepin linux学习笔记

    目录 deepin linux学习笔记 前言 linux常用命令 ls 显示文件夹内容 cd 切换当前目录 pwd 查看当前工作目录 mkdir 新建文件夹 rm 删除文件或文件夹 mv 移动文件 c ...

  2. 深圳市共创力推出独家课程《AHB和OSG》高级实务培训课程!

     <AHB和OSG>高级实务培训课程大纲 [适合对象]:高层管理者.产品经理.资源经理.各专项经理.研发等部门的负责人和骨干员工. [课程受益]:高层如何对项目的优先级进行排序和资源分配. ...

  3. Linux-Redmine安装方法

    Linux-Redmine安装方法 QQ群交流:585499566 一.环境准备 1,Linux系统:centos6.5 2,Redmine安装包:bitnami-redmine-3.4.6-0-li ...

  4. 移动端键盘密码输入框插件(jquery用于支付密码)

    最后生成样子: 配置值: * back {function} 回调函数 * msghtml {html} 自定义的html * title {string|object} 标题 * {txt:标题,b ...

  5. html页面打开ie浏览器默认打开最高版本

    <meta http-equiv="X-UA-Compatible" content="IE=edge" />

  6. .NET方法无限传参数技术

    是否有这样的需求在创建函数时参数个数不固定,又不想使用重载,那么下面这个技术就比较适合. 相信你一定见过下面这的代码: ); Format 就是string的一个函数,第一个参数是固定的字符串类型,那 ...

  7. linq中如何在join中指定多个条件

    public ActionResult Edit(int id) { using (DataContext db = new DataContext(ConfigurationManager.Conn ...

  8. 关于ES6的module的循环加载

    今天写js时,碰到了一个模块循环加载的错误,下面时例子: // testa.mjs import testb from './testb.mjs'; const {b} = testb; const ...

  9. ModuleNotFoundError: No module named 'redis'

    在安装过Redis后,通过Python程序导入redis时,遇到一个“ModuleNotFoundError: No module named redis”错误,网上查了下原因,解决办法如下: Pyt ...

  10. Linux C启动时创建pid文件

    程序在启动时将pid写入文件,当程序再次启动时会进行检测,避免启动多个实例. util-pidfile.h文件 #ifndef __UTIL_PID_H__ #define __UTIL_PID_H_ ...