(数组) leetcode 66. Plus One
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的更多相关文章
- 前端与算法 leetcode 66. 加一
目录 # 前端与算法 leetcode 66. 加一 题目描述 概要 提示 解析 解法一 解法二 算法 # 前端与算法 leetcode 66. 加一 题目描述 给定一个由整数组成的非空数组所表示的非 ...
- [LeetCode]66. 加一(数组)
###题目 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示例 ...
- LeetCode初级算法之数组:66 加一
加一 题目地址:https://leetcode-cn.com/problems/plus-one/ 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一.最高位数字存放在数组的首位, 数 ...
- 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 ...
- 2017-3-7 leetcode 66 119 121
今天纠结了一整天============================================================== leetcode66 https://leetcode.c ...
- leetcode 66
66. Plus One Given a non-negative number represented as an array of digits, plus one to the number. ...
- [LeetCode] 66. Plus One 加一
Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The ...
- Java实现 LeetCode 66 加一
66. 加一 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示 ...
- Java [Leetcode 66]Plus One
题目描述: Given a non-negative number represented as an array of digits, plus one to the number. The dig ...
随机推荐
- 【NodeJS】基础知识
nodejs基础 nodejs允许自己封装模块,使得编写程序可以模块化,便于维护整理.在一个js文件中写完封装的函数或对象后,可以使用exports或module.exports来将模块中的函数暴露给 ...
- WinForm 国际化的一些问题
国际化 我之前 WinForm 国际化都是凑一些代码搞起(请看文后 Reference). 最近发现还有个官方国际化方法: 首先设置 Form 的 Localizable 属性为 true 选择 Fo ...
- 【已采纳】最快获取package和activity的方式
意外找到一个本人自认为是最快获取package和activity的方法,欢迎来辩! 用adb命令快速查看某应用appPackage及appActivity的方法(前提是需要用数据线连接真机\模拟器也可 ...
- 手把手教新手小白在window把自己的项目上传到github
作为一个开发者,写博客,上传项目到github好像是不可不会的技能,很多有经验的老司机都会这么建议你.本宝宝第一次要把项目传到github的时候,确实有点蒙蔽,什么鬼,传个东西有必要这么难吗? git ...
- ORA-02266错误的批量生成脚本解决方案
ORA-02266: unique/primary keys in table referenced by enabled foreign keys这篇博客是很早之前总结的一篇文章,最近导数时使用TR ...
- asp.net webapi 的Request如何获取参数
public class BaseApiController : ApiController { private HttpRequestBase _request; /// 全局Requests对象 ...
- about-php
鉴于本人收集的php资料多,感觉查询起来不怎么方便.特意在github上建立了一个分支:about-php 主要是介绍围绕php的相关资料,包括php入门知识,php框架,开发工具,php项目,php ...
- 浅论Python密文输入密码的方法
近来做作业(老男孩那个9.9元的训练营)我想写一个装逼点的密文输入密码,类似于: 这个东西我先前实现过,忘了获取一个字节的方法是什么,于是去网上找,发现网上的实现方式大部分都有问题. 一.网上(百度) ...
- Docker平台的基本使用方法
1.运行一个 container并加载镜像centos,运行起来这个实例后,在实例中执行 /bin/bash命令 docker常用参数: run 运行 -i 以交互模式运行容器,通常与 -t 同时 ...
- 微信连wifi认证
官网 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444894086 https://blog.csdn.net/u0116 ...