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. 编写更好的C#代码

    引言 开发人员总是喜欢就编码规范进行争论,但更重要的是如何能够在项目中自始至终地遵循编码规范,以保证项目代码的一致性.并且团队中的所有人都需要明确编码规范所起到的作用.在这篇文章中,我会介绍一些在我多 ...

  2. [译]Exploring Angular 1.3: Binding to Directive Controllers

    原文: http://blog.thoughtram.io/angularjs/2015/01/02/exploring-angular-1.3-bindToController.html Angul ...

  3. JS替换函数

    var id= id.replace(/\,/g, "','"); 记一下,

  4. Python网络socket学习

    Python 网络编程 Python 提供了两个级别访问的网络服务.: 低级别的网络服务支持基本的 Socket,它提供了标准的 BSD Sockets API,可以访问底层操作系统Socket接口的 ...

  5. 东京区域2012-2014主要消费产品价格参考表——Excel

    声明: 1.本表格数据取自<日本の統計 2016>: 2.本表所有价格单位为人民币,其日元均以当年平均汇率兑换为此人民币价格: 3.其人民币—日元年均汇率数据取自IMF Data Exch ...

  6. 第四章第四个例题(LRJ)

    半年了,最起码的编程能力也谈不上啊,思维神马就更不不敢说了. 互联网时代讲求效率,走得慢和不走没有区别了. The war is on. (buhuidetiduokanduodajibianyehu ...

  7. [Asp.net MVC]Asp.net MVC5系列——第一个项目

    目录 概述 创建第一个项目 添加控制器 总结 概述 本教程是个人一步一步学习的总结,希望能帮到正在进入ASP.Net MVC5方向的朋友,个人也是准备进入ASP.NET MVC5领域,虽然艰辛,但是乐 ...

  8. 真有用?Snap和Flatpak 通吃所有发行版的打包方式。

    导读 最近我们听到越来越多的有关于Ubuntu的Snap包和由Red Hat员工Alexander Larsson创造的 Flatpak (曾经叫做 xdg-app)的消息.这两种下一代打包方法在本质 ...

  9. unity销毁层级物体及 NGUI 深度理解总结

    http://www.2cto.com/kf/201311/258811.html 1.想找到层级面板中某个物体,并销毁,利用下面的代码:    GameObject  obj = GameObjec ...

  10. dispaly:table-cell,inline-block,阐述以及案例

    display:table 此元素会作为块级表格来显示(类似 <table>),表格前后带有换行符.dispaly:table-row 此元素会作为一个表格行显示(类似 <tr> ...