【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-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- windows系统添加删除用户命令!
参考:net help usernet help group Net user添加或修改用户帐户或者显示用户帐户信息. 语法net user [UserName [Pa ...
- iframe框架嵌套技巧(全屏,去双滚动条)
一般情况下我们很少用到iframe(框架),但有些特殊的情况下我们不得不使用iframe,那么或许或遇到嵌套内容不全屏,网页周围有边框,双滚动条等等情况,下面来说一下处理技巧. 全屏与边框处理: &l ...
- 使用Javascript实现返回顶部功能。
为了提高网站的浏览体验及友好度,相信大部分网站需要一个返回顶部的按钮,如果使用传统的a标记,再做一个div加上链接的话,非常麻烦,不仅每个页面都需要添加,而且不能实现非常智能的效果及简化维护时间. 下 ...
- ios修改textField的placeholder的字体颜色和大小
textField.placeholder = @"username is in here!"; [textField setValue:[UIColor redColor] fo ...
- MAVEN for mac 安装
http://blog.csdn.net/anialy/article/details/22217937 下载 maven http://mirrors.hust.edu.cn/apache/mav ...
- 如何修改windows远程端口
Windows系统默认远程桌面端口是3389,修改方法: 远程登陆服务器,运行中使用"regedit"命令打开注册表编辑器,依次展开"HKEY_LOCAL_MACHINE ...
- 校友聊NABCD
特点之一 界面简洁 N:软件的界面是软件成功的必要条件,界面简洁,用户使用方便,就会吸引用户. A:界面可用多种做法做,暂定用C# B:简洁的界面,用户易于理解各项功能,方便使用. C:没有其 ...
- NSLock/NSRecursiveLock/NSConditionLock/@synchronized
NSLock/NSRecursiveLock/NSConditionLock/@synchronized http://blog.sina.com.cn/s/blog_8c87ba3b0101ok8y ...
- Understand:高效代码静态分析神器详解(转)
之前用Windows系统,一直用source insight查看代码非常方便,但是年前换到mac下面,虽说很多东西都方便了,但是却没有了静态代码分析工具,很幸运,前段时间找到一款比source ins ...
- python\c交互学习网站手机
http://use-python.readthedocs.org/zh_CN/latest/interact_with_other_language.html https://www.zhihu.c ...