【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 ...
随机推荐
- MATLAB之数学建模:深圳市生活垃圾处理社会总成本分析
MATLAB之数学建模:深圳市生活垃圾处理社会总成本分析 注:MATLAB版本--2016a,作图分析部分见<MATLAB之折线图.柱状图.饼图以及常用绘图技巧> 一.现状模式下的模型 % ...
- Android中渐变图片失真的解决方案
在android开发(尤其是android游戏开发)中有一个很严重的问题就是带有渐变效果的png图片会出现严重的banding(色带),鉴于这种情况,有几种可行的解决方法: 1.如果Activit ...
- IntelliJ IDEA IDEA 2018 激活注册码
K03CHKJCFT-eyJsaWNlbnNlSWQiOiJLMDNDSEtKQ0ZUIiwibGljZW5zZWVOYW1lIjoibnNzIDEwMDEiLCJhc3NpZ25lZU5hbWUiO ...
- Sentinel spring-cloud-gateway adapter(1.6)异常错误之@EnableCircuitBreaker
sentinal 大坑 使用gateway adaper包出现@EnableCircuitBreaker did you forget include starter的异常 这时候千万不要学我引入cl ...
- innobackupex备份脚本
#!/bin/bash # 10 23 * * * /bin/bash /data/script/backup.sh BDATE=`date +%Y%m%d%H%M%S`BPATH=/data/bac ...
- fflush - 刷新一个流
SYNOPSIS 总览 #include <stdio.h> int fflush(FILE *stream); DESCRIPTION 描述 函数 fflush 强制在所给的输出流或更新 ...
- 数组逆序=全局内存版 VS 共享内存版
全局内存版 #include <stdio.h> #include <assert.h> #include "cuda.h" #include " ...
- CUDA:Supercomputing for the Masses (用于大量数据的超级计算)-第三节
原文链接 第三节:错误处理和全局内存性能局限 恭喜!通过对CUDA(Compute Unified DeviceArchitecture,即计算统一设备架构的首字母缩写)系列文章第一节和第二节,您现在 ...
- 使用git stash命令保存和恢复进度
使用git stash命令保存和恢复进度 git stash 保存当前工作进度,会把暂存区和工作区的改动保存起来.执行完这个命令后,在运行git status命令,就会发现当前是一个干净的工作区,没有 ...
- caller、callee的用法及区别
1 :caller 返回一个调用当前函数的引用 如果是由顶层调用的话 则返回null (举个栗子哈 caller给你打电话的人 谁给你打电话了 谁调用了你 很显然是下面a函数的执行 只有在打电话的时 ...