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. Your project path contains non-ASCII characters

    Android studio导入project时报错 non-ASCII characters意味着中文字符报错,解决方法简单有效: 检查项目路径中是否出现中文名,将中文字符修改成英文就可以解决辽~

  2. 2D射影几何和变换

    阅读<计算机视觉中的多视图集合> 2D射影几何和变换 2D射影平面 本章的关键是理解线和点的对偶性.从射影平面模型出发,IP^2^内的点(a, b ,c)由IP^3^空间中一条过原点的射线 ...

  3. 使用 phpstudy 搭建本地测试环境

    最近在为另一个部门配置一个多语言的网站,因为之前他们已经做过 英文和中文两种语言,这次帮他们添加其它几种语言,从GitLab 上拉下来的代码,是php环境做的,需要在本地跑起来,做完测试通过后再一次性 ...

  4. sqlserver 2014使用时有Cannot find one or more components

    好久没用sqlserver,今天打开却出现了一个错误,Cannot find one or more components,令人头疼.在启动Microsoft SQL Server Managemen ...

  5. docker中镜像的提交和上传

    本文介绍如何将本地的镜像上传到镜像仓库.以及上传时遇到"denied: requested access to the resource is denied"的解决方法. 原文地址 ...

  6. C# -- 使用线程池 ThreadPool 执行多线程任务

    C# -- 使用线程池 ThreadPool 执行多线程任务 1. 使用线程池 class Program { static void Main(string[] args) { WaitCallba ...

  7. ASP.NET MVC 下自定义 ModelState 扩展类,响应给 AJAX

    ModelStateExtensions.cs using System.Collections.Generic; using System.Linq; using System.Web.Mvc; n ...

  8. 试试Linux下的ip命令,ifconfig已经过时了

    linux的ip命令和ifconfig类似,但前者功能更强大,并旨在取代后者.使用ip命令,只需一个命令,你就能很轻松地执行一些网络管理任务.ifconfig是net-tools中已被废弃使用的一个命 ...

  9. redis 初步认识二(c#调用redis)

    前置:服务器安装redis 1.引用redis 2.使用redis(c#) 一 引用redis  (nuget   搜索:CSRedisCore) 二 使用redis(c#) using System ...

  10. 初识gauge自动化测试框架

    segmentfault阅读 官方网站:https://docs.gauge.org/latest/index.html 介绍: Gauge是一个轻量级的跨平台测试自动化工具,可以使用不同的语言中编写 ...