lintcode407 加一
加一
给定一个非负数,表示一个数字数组,在该数的基础上+1,返回一个新的数组。
该数字按照大小进行排列,最大的数在列表的最前面。
给定 [1,2,3] 表示 123, 返回 [1,2,4].
给定 [9,9,9] 表示 999, 返回 [1,0,0,0].
第一种方法:
class Solution {
public:
/*
* @param digits: a number represented as an array of digits
* @return: the result
*/
vector<int> plusOne(vector<int> &digits) {
// write your code here
vector<int> res;
if (digits.empty()) {
return res;
}
int len = digits.size();
long num = ;
for (int i = ; i < len; i++) {
num = num * + digits[i];
}
num = num + ;
while (num) {
int temp = num % ;
res.push_back(temp);
num = num / ;
}
reverse(res.begin(), res.end());
return res;
}
};
第二种方法:
class Solution
{
public:
/**
* @param digits a number represented as an array of digits
* @return the result
*/
vector<int> plusOne(vector<int>& digits)
{
int c = ; for (int i = digits.size() - ; i >= ; i--)
{
digits[i] = digits[i] + c;
c = digits[i]/;
digits[i] %= ;
} if (c > )
{
vector<int> temp(digits.size()+, );
temp[] = c;
for (int i = ; i < temp.size(); i++)
{
temp[i] = digits[i - ];
}
return temp;
} else
{
return digits;
}
}
};
lintcode407 加一的更多相关文章
- 百度推出新技术 MIP,网页加载更快,广告呢?
我们在2016年年初推出了MIP,帮助移动页面加速(原理).内测数据表明,MIP页面在1s内加载完成.现在已经有十多家网站加入MIP项目,有更多的网站正在加入中.在我们收到的反馈中,大部分都提到了广告 ...
- UWP中新加的数据绑定方式x:Bind分析总结
UWP中新加的数据绑定方式x:Bind分析总结 0x00 UWP中的x:Bind 由之前有过WPF开发经验,所以在学习UWP的时候直接省略了XAML.数据绑定等几个看着十分眼熟的主题.学习过程中倒是也 ...
- 探真无阻塞加载javascript脚本技术,我们会发现很多意想不到的秘密
下面的图片是我使用firefox和chrome浏览百度首页时候记录的http请求 下面是firefox: 下面是chrome: 在浏览百度首页前我都将浏览器的缓存全部清理掉,让这个场景最接近第一次访问 ...
- 高性能Javascript--脚本的无阻塞加载策略
Javascript在浏览器中的性能,可以说是前端开发者所要面对的最重要的可用性问题. 在Yahoo的Yslow23条规则当中,其中一条是将JS放在底部 .原因是,事实上,大多数浏览器使用单进程处理U ...
- 为你的Web程序加个启动画面
.Net开发者一定熟悉下面这个画面: 这就是宇宙第一IDE Visual Studio的启动画面,学名叫Splash Screen(或者Splash Window).同样,Javar们一定对Eclip ...
- 懒加载session 无法打开 no session or session was closed 解决办法(完美解决)
首先说明一下,hibernate的延迟加载特性(lazy).所谓的延迟加载就是当真正需要查询数据时才执行数据加载操作.因为hibernate当中支持实体对象,外键会与实体对象关联起来.如 ...
- Bootstrap-Select 动态加载数据的小记
关于前端框架系列的可以参考我我刚学Bootstrap时候写的LoT.UI http://www.cnblogs.com/dunitian/p/4822808.html#lotui bootstrap- ...
- 按需加载.js .css文件
首先,理解按需加载当你需要用到某个js里面的函数什么鬼,或者某个css里的样式的时候你才开始加载这个文件. 然后是怎样实现的,简单来说就是在js中动态的createElem<script> ...
- Vue-Router 页面正在加载特效
Vue-Router 页面正在加载特效 如果你在使用 Vue.js 和 Vue-Router 开发单页面应用.因为每个页面都是一个 Vue 组件,你需要从服务器端请求数据,然后再让 Vue 引擎来渲染 ...
随机推荐
- 【题解】洛谷P4391 [BOI2009] Radio Transmission(KMP)
洛谷P4391:https://www.luogu.org/problemnew/show/P4391 思路 对于给定的字符串 运用KMP思想 设P[x]为前x个字符前缀和后缀相同的最长长度 则对于题 ...
- Multicast Routing
Multicasting Source S sends packets to multicast group G1 (and minimize the number of copies) Revers ...
- Gradle Goodness: Adding Tasks to a Predefined Group
In Gradle we can group related tasks using the group property of a task. We provide the name of our ...
- Lua库-bit32库
Global = Global or {}; local bits = {}; function bits.bxor(num1,num2) local ret=bit32.bxor(num1,num2 ...
- 课时47.datalist标签(了解)
1.datalist标签 作用:给输入框绑定待选项 2.datalist格式: <datalist> <option>待选项内容</option> </dat ...
- 关于ORA-00257: archiver error. Connect internal only, until freed 错误的处理方法
转 关于ORA-00257: archiver error. Connect internal only, until freed 错误的处理方法 2016年03月31日 10:14:59 阅读数:1 ...
- oracle表空间的创建+权限分配
/*分为四步 */ /*第1步:创建临时表空间 */ create temporary tablespace user_temp tempfile 'D:\oracle\oradata\Oracle9 ...
- Use PerfHUD ES to Do Frame Capture Android Game
Author: http://www.cnblogs.com/open-coder/p/3898224.html Get Start This is short tutorial about how ...
- shell入门基础&常见命令及用法
shell shell是一个命令解释器,实际是一个程序,/bin/bash,linux中所有的命令都由它来解释,有自己的语法 shell脚本 以.sh结尾 shell语法+linux命令 注释: 单行 ...
- 分析nginx 日志常用命令
一.概念 并发连接数 客户端向服务器发起请求,并建立了TCP连接.每秒钟服务器链接的总TCP数量,就是并发连接数.请求数 请求数指的是客户端在建立完连接后,向http服务发出GET/POS ...