题目

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的更多相关文章

  1. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  2. 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~ ...

  3. 【Valid Sudoku】cpp

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  4. 【Permutations II】cpp

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  5. 【Subsets II】cpp

    题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  6. 【Sort Colors】cpp

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  7. 【Sort List】cpp

    题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...

  8. 【Path Sum】cpp

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  9. 【Symmetric Tree】cpp

    题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...

  10. 【Same Tree】cpp

    题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...

随机推荐

  1. Eucalyptus简介

    1.Eucalyptus是什么? Eucalyptus  n.桉树 Eucalyptus is a Linux-based software architecture that implements ...

  2. iOS 解决tableView中headerView头部视图不跟随tableView滑动的方法

    解决方法如下: if (scrollView.contentOffset.y >= 0 && scrollView.contentOffset.y <= pushNewsT ...

  3. 在MVC中加载view(点开链接)的方式

    主要有: Html.ActionLink Html.RenderPartial Html.RenderAction Html.Partial AJAX.ActionLink load 浏览器对象模型 ...

  4. LeetCode Add Binary 两个二进制数相加

    class Solution { public: string addBinary(string a, string b) { if(a==""&&b==" ...

  5. 微软高性能缓存AppFabric(二)使用

    原文链接:http://www.cnblogs.com/Qbit/p/6102614.html 从AppFabric 的安装目录中选择两个dll添加到项目中, 默认安装位置:C:\Program Fi ...

  6. PHP:isset()-检测变量是否被设置

    isset()-检测变量是否被设置 bool isset(mixed $var [, mixed $...]),检查变量是否被设置,并且不是NULL.var,要检测的变量,...其他变量,允许有多个变 ...

  7. 新建framework的bundle资源 图片资源被编译成了ttf后缀 解決

    设置combine_hidpi_images为no

  8. POJ 1703 Find them, Catch them(并查集,等价关系)

    DisjointSet保存的是等价关系,对于某个人X,设置两个变量Xa,Xb.Xa表示X属于a帮派,Xb类似. 如果X和Y不是同一个帮派,那么Xa -> Yb,Yb -> Xa... (X ...

  9. Github的基本功能

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:Fadeoc Khaos链接:http://www.zhihu.com/question/20070065/answer/30 ...

  10. 操作系统(1)_操作系统结构_李善平ppt

    cpu和内存之间通过地址总线.数据总线.控制总线连接.外部总线连接外部设备.下图有问题,内存和外设没有直接连接.同一组总线,CPU和内存连接的时候硬盘就不能和内存连接,否则有冲突,core和core之 ...