[LintCode] Plus One 加一运算
Given a non-negative number represented as an array of digits, plus one to the number.
The digits are stored such that the most significant digit is at the head of the list.
Given [1,2,3] which represents 123, return[1,2,4].
Given [9,9,9] which represents 999, return[1,0,0,0].
LeetCode上的原题,请参见我之前的博客Plus One。
解法一:
class Solution {
public:
/**
* @param digits a number represented as an array of digits
* @return the result
*/
vector<int> plusOne(vector<int>& digits) {
vector<int> res;
int carry = , n = digits.size();
for (int i = n - ; i >= ; --i) {
int sum = digits[i] + carry;
res.insert(res.begin(), sum % );
carry = sum / ;
}
if (carry == ) res.insert(res.begin(), );
return res;
}
};
解法二:
class Solution {
public:
/**
* @param digits a number represented as an array of digits
* @return the result
*/
vector<int> plusOne(vector<int>& digits) {
for (int i = digits.size() - ; i >= ; --i) {
if (digits[i] < ) {
digits[i] += ;
return digits;
}
digits[i] = ;
}
digits.insert(digits.begin(), );
return digits;
}
};
[LintCode] Plus One 加一运算的更多相关文章
- [LeetCode] Plus One Linked List 链表加一运算
Given a non-negative number represented as a singly linked list of digits, plus one to the number. T ...
- 一个线程加一运算,一个线程做减一运算,多个线程同时交替运行--synchronized
使用synchronized package com.pb.thread.demo5; /**使用synchronized * 一个线程加一运算,一个线程做减法运算,多个线程同时交替运行 * * @a ...
- velocity加减运算注意格式 ,加减号的左右都要有空格
velocity加减运算注意格式 ,加减号的左右都要有空格 #set( $left= $!biz.value - $vMUtils.getReturnMoney($!biz.billBuy) )
- [Swift]LeetCode592. 分数加减运算 | Fraction Addition and Subtraction
Given a string representing an expression of fraction addition and subtraction, you need to return t ...
- C语言中指针变量的加减运算
1.指针变量中存放的是地址值,也就是一个数字地址,例如某指针变量中的值是0x20000000,表示表示此指针变量存放的是内存中位于0x20000000地方的内存地址.指针变量可以加减,但是只能与整型数 ...
- 大整数加减运算的C语言实现
目录 大整数加减运算的C语言实现 一. 问题提出 二. 代码实现 三. 效果验证 大整数加减运算的C语言实现 标签: 大整数加减 C 一. 问题提出 培训老师给出一个题目:用C语言实现一个大整数计算器 ...
- Linux中日期的加减运算
Linux中日期的加减运算 目录 在显示方面 在设定时间方面 时间的加减 正文 date命令本身提供了日期的加减运算. date 可以用来显示或设定系统的日期与时间. 回到顶部 在显示方面 使用者可以 ...
- void *指针的加减运算
1.手工写了一个程序验证void *指针加减运算移动几个字节: //本程序验证空类型指针减1移动几个字节 #include <stdio.h> int main(int argc, cha ...
- Leetcode 592.分数加减运算
分数加减运算 给定一个表示分数加减运算表达式的字符串,你需要返回一个字符串形式的计算结果. 这个结果应该是不可约分的分数,即最简分数. 如果最终结果是一个整数,例如 2,你需要将它转换成分数形式,其分 ...
随机推荐
- WPF线程(Step2)——BackgroundWorker
在WPF中第二个常用的线程处理方式就是BackgroundWorker. 以下是BackgroundWorker一个简单的例子. public partial class MainWindow : W ...
- 在Window的IIS中创建FTP的Site并用C#进行文件的上传下载
文件传输协议 (FTP) 是一个标准协议,可用来通过 Internet 将文件从一台计算机移到另一台计算机. 这些文件存储在运行 FTP 服务器软件的服务器计算机上. 然后,远程计算机可以使用 FTP ...
- Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA/(树链剖分+数据结构) + MST
E. Minimum spanning tree for each edge Connected undirected weighted graph without self-loops and ...
- 关于移动端1px边框问题
<div class="z_nei_list"> <div class="z_name_left font-size3">身份证号:&l ...
- 生成n位随机字符串
--1.借助newid() Go --创建视图(因为在函数中无法直接使用newid()) create view vnewid as select newid() N'MacoId'; go --创建 ...
- How to choose the number of topics/partitions in a Kafka cluster?
This is a common question asked by many Kafka users. The goal of this post is to explain a few impor ...
- 【Tomcat 系统服务】将tomcat设置为系统服务,并且开机自启 + 卸载tomcat服务
1.首先 你得下载一个tomcat[一般都是解压版的,解压放在那里就能用] startup.bat shutdown.bat service.bat等文件都在tomcat的bin目录下 ,例如 ...
- MySQL导入sql脚本 导出数据库
导出数据库 不能停止服务 cd /var/lib/mysql (进入到MySQL库目录,根据自己的MySQL的安装情况调整目录) mysqldump -u用户名 -p 数据库名 > 导出的文件名 ...
- 制作caffe中的test.txt和val.txt
find -name *.jpeg |cut -d '/' -f2-3> train.txt(图片在当前文件夹) find train/dog -name *.JPEG |cut -d '/' ...
- 关于CSS动画效果的图片展示
animation:帧动画 animation-name:定义绑定Keyframes的动画名称 @keyframes XXX 定义动画,里面是动画具体内容 animation-duration:过渡动 ...