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)的更多相关文章

  1. 【leetcode】 Unique Path ||(easy)

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  2. 【leetcode】triangle(easy)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  3. 【leetcode】Implement strStr() (easy)

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  4. 【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 ...

  5. 【leetcode】Valid Sudoku (easy)

    题目:就是判断已有的数字是否冲突无效,若无效返回flase 有效返回true 不要求sudo可解 用了char型的数字,并且空格用‘.'来表示的. 思路:只要分别判断横向 竖向 3*3小块中的数字是否 ...

  6. 【leetcode】Two Sum (easy)

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  7. 【leetcode】Majority Element (easy)(*^__^*)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  8. 【leetcode】Palindrome Number (easy)

    Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...

  9. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

随机推荐

  1. 通过google chrome操作JavaScript中Console

    紧接着有关上一个文章的!function................. 前端开发人员一定会用到你的开发者工具中的Console控制台.通常Console用于调试程序,日志输出,打断点等功能.比如我 ...

  2. sql中的常见的全局变量

    select APP_NAME ( ) as w --当前会话的应用程序 select @@IDENTITY --返回最后插入的标识值 select USER_NAME() --返回用户数据库用户名 ...

  3. 网站SEO优化之添加Sitemap文件。

    Sitemap.xml 故名思意就是站点地图文件,可以指引Google spider 收录相应网页.正确地使用Google Sitemap,可以确保让Google spider 不遗漏网站内的任何页面 ...

  4. R语言 小程序

    x<-sample(1:11) x 日期和时间 ###Data and Time### > data_time <- data.frame(data = c("2015-0 ...

  5. 统计学 nested_design 嵌套设计

    nested_design 嵌套设计 li_volleyball ,邓邦良 2016年3月6日 嵌套设计 一.基本概念 嵌套设计(nested design)又称为窝设计和套设计,与析因设计的处理不同 ...

  6. hdu.1226.超级密码(bfs)

    超级密码 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  7. maven 工程启动找不到 Spring ContextLoaderListener 的解决办法

    1.错误:  Error configuring application listener of class org.springframework.web.context.ContextLoader ...

  8. Ubuntu 下apache2开启rewrite隐藏index.php

    为了实现 http://www.example.com/route/route 而不是 http://www.example.com/index.php/route/route 需要开启apache2 ...

  9. centos 6.5 zabbix3.0.4 监控apache

    开启apache的server-status httpd.conf 末尾添加 [root@test3 /]# vim /usr/local/httpd-/conf/httpd.conf Extende ...

  10. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...