题目

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. echo -e的用法

    root@bt:~# echo -e "HEAD /HTTP/1.0\n\n"HEAD /HTTP/1.0 root@bt:~# echo -e "HEAD /HTTP/ ...

  2. 永洪BI——国内领军的一站式大数据分析平台

    平台: CentOS 类型: 虚拟机镜像 软件包: jdk-7.79-linux yonghongbi.sh basic software big data business intelligence ...

  3. python3.7 安装 xlrd 模块---Mac

    要用Excel将数据和代码分离,需要import xlrd, 使用前需要先安装xlrd模块. 说明:通过在google中搜索“xlrd Mac”,下载xlrd.py模块(下载地址:http://mac ...

  4. UML中类图(Class Diagram)的关系整理

    什么是UML类图? 类图显示了一组类.接口.协作以及他们之间的关系.在UML中问题域最终要被逐步转化,通过类来建模,通过编程语言构建这些类从而实现系统.类加上他们之间的关系就构成了类图,类图中还可以包 ...

  5. Mysql数据库插入时乱码问题解决

    我们在利用cmd的黑屏界面进行mysql数据的插入时往往会出现不能插入的情况,这个原因是因为我们系统虽然和服务器端即mysql的数据库采用的都是统一的utf8的编码,但是在传输的过程中会变成iso88 ...

  6. 制作X509证书

    makecert -r -pe -n "CN=XXX" -b 01/01/2005 -e 01/01/2020 -sky exchange -ss my

  7. Aizu 2301 Sleeping Time(概率,剪枝)

    根据概率公式dfs即可,判断和区间[T-E,T+E]是否有交,控制层数. #include<bits/stdc++.h> using namespace std; int K,R,L; d ...

  8. hdu-1068&&POJ1466 Girls and Boys---最大独立集

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1068 题目大意: 有n个人,一些人认识另外一些人,选取一个集合,使得集合里的每个人都互相不认识,求该 ...

  9. Finite Encyclopedia of Integer Sequences(找规律)

    6617: Finite Encyclopedia of Integer Sequences 时间限制: 1 Sec  内存限制: 128 MB提交: 375  解决: 91[提交] [状态] [讨论 ...

  10. js 实现纯前端将数据导出excel两种方式,亲测有效

    由于项目需要,需要在不调用后台接口的情况下,将json数据导出到excel表格,兼容chrome没问题,其他还没有测试过 通过将json遍历进行字符串拼接,将字符串输出到csv文件,输出的文件不会再是 ...