【leetcode】Plus One (easy)
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.
分析:思路是很清晰的,就是从数字最低位开始向上循环,如果是9就变成0,如果不是就直接当前位加1,返回。如果全都是9,就在最高位加一个1.
但是我写C++的代码比较少,写的时候对于哪里是最高位弄晕了。绕了好久才AC。
高位在前,就是高位是先压入vector的,那么最高位在digits.begin()。先压入的靠近下标0
如果需要新加一个最高位,那digits里面肯定都是0,压入1后,最高位变成最后压入的了,所以还需要翻转一下。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
int i = digits.size() - ;
while(i >= )
{
if(digits[i] == )
{
digits[i] = ;
i--;
}
else
{
break;
}
}
if(i < )
{
digits.push_back();
reverse(digits.begin(), digits.end());
}
else
{
digits[i] += ;
} return digits;
}
}; int main()
{
Solution s;
vector<int> in, out;
in.push_back();
in.push_back();
in.push_back();
out = s.plusOne(in); return ;
}
【leetcode】Plus One (easy)的更多相关文章
- 【leetcode】 Unique Path ||(easy)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 【leetcode】triangle(easy)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- 【leetcode】Implement strStr() (easy)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 【leetcode】Climbing Stairs (easy)
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- 【leetcode】Valid Sudoku (easy)
题目:就是判断已有的数字是否冲突无效,若无效返回flase 有效返回true 不要求sudo可解 用了char型的数字,并且空格用‘.'来表示的. 思路:只要分别判断横向 竖向 3*3小块中的数字是否 ...
- 【leetcode】Two Sum (easy)
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- 【leetcode】Majority Element (easy)(*^__^*)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【leetcode】Palindrome Number (easy)
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- 认识ATL窗口
这是一个相当于“Hello world!”的任务,作为认识ATL,考查了其运作流程与机制. 环境:VS2008 创建:新建-项目-Win32项目-添加公用头文件用于(选择ATL). PS:注意新建项目 ...
- 数据库左连接left join、右连接right join、内连接inner join on 及 where条件查询的区别
join on 与 where 条件的执行先后顺序: join on 条件先执行,where条件后执行:join on的条件在连接表时过滤,而where则是在生成中间表后对临时表过滤 left joi ...
- js(jquery)代码在页面上实时地显示时间
一.引入jquery 二.HTML代码 三.js代码 1)引入js代码 2)下面是完整的js代码
- ajax状态
ajax的几个状态 Uninitialized 初始化状态.XMLHttpRequest 对象已创建或已被 abort() 方法重置. Open open() 方法已调用,但是 send() 方法未调 ...
- test dword ptr [eax],eax ; probe page. visual studio
当在函数中申请占用空间很大的数组.结构体时,会产生该问题. 由于局部变量的申请空间存放于栈中,windows里默认栈内存是1M 所以当申请空间大于1M时就会出现溢出错误 通过debug就会进入以下文件 ...
- js实现自定义右键菜单--兼容IE、Firefox、Chrome
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- 在C语言源程序中的格式字符与空格等效
#include <stdio.h> #\ i\ n\ c\ l\ u\ d\ e \ <\ s\ t\ d\ l\ i\ b\ .\ h\ > /* *预处理指令这里换行符会 ...
- Android中的“再按一次返回键退出程序”实现
用户退出应用前给出一个提示是很有必要的,因为可能是用户并不真的想退出,而只是一不小心按下了返回键,大部分应用的做法是在应用退出去前给出一个Dialog,我觉得这样不太友好,用户还得移动手指去按dial ...
- 架构Android App总结
历时两个多月,自己架构的一个App快要完成了,有很多可以总结的地方: 1, 各个模块尽可能独立,不要直接调用,用消息机制解耦.包括页面跳转不要直接startActivity,而是用消息跳转:业务模块请 ...
- Python自动化之IO多路复用
单线程.多线程和异步对比图 灰色的是阻塞 IO多路复用 用户空间与内核空间 现在操作系统都是采用虚拟存储器,那么对32位操作系统而言,它的寻址空间(虚拟存储空间)为4G(2的32次方).操作系统的核心 ...