(数组) 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 ...
随机推荐
- iframe中的a标签电话链接不能正常打开
背景 经测试,android手机中没有这个问题, iphone手机中的Safari浏览器会出现这个问题. 例如: <a href = "tel://1-408-555-5555&quo ...
- Python笔记-高级特性
1.迭代 如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们称为迭代(Iteration). 如果要迭代value,可以用for value in d ...
- windows下vagrant的安装使用
vagrant是简便虚拟机操作的一个软件,而使用虚拟机有几个好处: 1.为了开发环境与生产环境一致(很多开发环境为windows而生产环境为linux),不至于出现在开发环境正常而移步到正式生产环境时 ...
- rabbitmq之确保消息不丢失
1.背景引入 在使用消息中间件(rabbitmq)时,令开发者最头痛的就是防止消息丢失问题,而消息丢失可能发生的位置主要为三种,分别为(1)消息发送到MQ中消费者消费未成功时突然宕机:(2)消息发送到 ...
- Java 基本文件操作
Java 文件操作 , 这也是基于Java API 操作来实现的. 文件是操作系统管理外存数据管理的基本单位, 几乎所有的操作系统都有文件管理机制. 所谓文件, 是具有符号名而且在逻辑上具有完整意义的 ...
- Unity切换到安卓平台Shader丢失(opengl)
Unity安卓平台shader平台丢失 Unity的工程切换到Android平台后,运行游戏出现shader丢失 解决办法:在Unity桌面图标的快捷方式后添加 -force-gles20 示例:&q ...
- requests库下载图片的方法
方法: 传入图片url,requests.get()方法请求一下,将源码以二进制的形式写在本地即可. 以前一直以为requests库中有特定的方法获取图片,类似urllib.request.urlre ...
- Vue组织架构图组件
vue-tree-chart :deciduous_tree: Vue2树形图组件 安装 npm i vue-tree-chart --save 使用 in template: <TreeC ...
- socket.io 出现的WebSocket is closed before the connection is established
WebSocket is closed before the connection is established 最近socket.io是挺流行的,幼麟棋牌和一些好的开源项目也使用这个框架,在搭建其平 ...
- [JSOI2008]Blue Mary的旅行
嘟嘟嘟 看\(n\)那么小,就知道是网络流.然后二分,按时间拆点. 刚开始我看成所有航班一天只能起飞一次,纠结了好一会儿.但实际上是每一个航班单独考虑,互不影响. 建图很显然,拆完点后每一个点的第\( ...