【Plus One】cpp
题目:
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.
代码:
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int carry = ;
for (std::vector<int>::reverse_iterator i = digits.rbegin(); i != digits.rend() && carry>; ++i)
{
const int tmp = *i+carry;
*i = tmp%;
carry = tmp/;
}
if ( carry > ) digits.insert(digits.begin(), carry);
return digits;
}
};
Tips:
高精度加法。
利用reverse_iterator来反向迭代vector。
========================================================
这种高精度的不要忘记最后一个的进位。
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
vector<int> ret;
int carry = ;
for ( int i=digits.size()-; i>=; --i )
{
int curr = digits[i] + carry;
carry = curr / ;
curr = curr % ;
ret.insert(ret.begin(), curr);
}
if ( carry!= ) ret.insert(ret.begin(), carry);
return ret;
}
};
【Plus One】cpp的更多相关文章
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- Hdu 4734 【数位DP】.cpp
题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...
- 【Valid Sudoku】cpp
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 【Permutations II】cpp
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- 【Subsets II】cpp
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- 【Sort Colors】cpp
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 【Sort List】cpp
题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...
- 【Path Sum】cpp
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- 【Symmetric Tree】cpp
题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...
- 【Same Tree】cpp
题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...
随机推荐
- 构建第一个Spring Boot2.0应用之RequestMapping(四)
在学习controller的时候,测试了在RequestMapping中,value参数中配置集合,实现不同的URL访问同一方法. 本章继续学习和测试RequestMapping的其他特性. 一.Pa ...
- Retrofit 2.0:有史以来最大的改进
因为其简单与出色的性能,Retrofit 是安卓上最流行的HTTP Client库之一. 不过它的缺点是在Retrofit 1.x中没有直接取消正在进行中任务的方法.如果你想做这件事必须手动杀死,而这 ...
- 高德地图 获取sha1
开发版本sha1 控制台输入 cd .android 回车 再输入 keytool -list -v -keystore debug.keystore 回车 输入密钥库口令: andorid ...
- Struct2标签的传值方式(转载)
"#request.userList"> "center"> "id"/> : "username"/ ...
- 【强力卸载】使用Uninstall Tool卸载各类不易卸载的软件
Uninstall Tool 经测试卸载MySql5.7.18成功. 下载地址: http://files.cnblogs.com/files/xiaohi/%E3%80%90%E8%BD%AF%E4 ...
- pat乙级1045
从左到右扫描时记录扫描到当前下标为止的最大值,如果当前元素大于这个最大值,那么它就大于它左边的所有值.同理,从右到左扫描记录扫描到当前下标为止的最小值,如果当前元素小于这个最大小值,那么它就小于它右边 ...
- POJ 3421 X-factor Chains(构造)
这条链依次乘一个因子.因为n<2^20,sqrt(n)分解因子,相同的因子相对顺序取一个. 组合公式计算一下就好. #include<cstdio> #include<iost ...
- 【洛谷4424】[HNOI/AHOI2018] 寻宝游戏(位运算思维题)
点此看题面 大致题意: 给你\(n\)个\(m\)位二进制数.每组询问给你一个\(m\)位二进制数,要求你从\(0\)开始,依次对于这\(n\)个数进行\(and\)或\(or\)操作,问有多少种方案 ...
- samba性能调优,调优后,性能增加30%
global中增加下面内容. [global] use sendfile = yes write raw = yes read raw = yes max xmit = 655 ...
- SQLAlchemy简介
一.SQLAlchemy简介 SQLAlchemy是Python SQL工具包和对象关系映射器,是python中最著名的ORM(Object Relationship Mapping)框架,它简化了应 ...