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后,最高位变成最后压入的了,所以还需要翻转一下。

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. class Solution {
  7. public:
  8. vector<int> plusOne(vector<int> &digits) {
  9. int i = digits.size() - ;
  10. while(i >= )
  11. {
  12. if(digits[i] == )
  13. {
  14. digits[i] = ;
  15. i--;
  16. }
  17. else
  18. {
  19. break;
  20. }
  21. }
  22. if(i < )
  23. {
  24. digits.push_back();
  25. reverse(digits.begin(), digits.end());
  26. }
  27. else
  28. {
  29. digits[i] += ;
  30. }
  31.  
  32. return digits;
  33. }
  34. };
  35.  
  36. int main()
  37. {
  38. Solution s;
  39. vector<int> in, out;
  40. in.push_back();
  41. in.push_back();
  42. in.push_back();
  43. out = s.plusOne(in);
  44.  
  45. return ;
  46. }

【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. linux下gedit读取txt乱码解决办法

    修改一下gedit的设置来让它显示的txt不再是乱码: 你可以通过以下步骤,使 gedit 正确显示中文编码文件. 按下 Alt-F2,打开“运行应用程序”对话框.在文本框中键入“gconf-edit ...

  2. CSS样式案例(1)-文字的排版

    本篇介绍的是小窗文字内容的排版,通过该篇文章可以让小伙伴们熟悉以下几个知识点: word-space.overflow.text-overflow. 最终的展示效果如下: 参考步骤: 1. 建立htm ...

  3. java之stream(jdk8)

    一.stream介绍 参考: Java 8 中的 Streams API 详解   Package java.util.stream   Java8初体验(二)Stream语法详解   二.例子 im ...

  4. 关于CSS的优先级,CSS优先级计算

    原则一: 继承不如指定原则二: #id > .class > 标签选择符原则三:越具体越强大原则四:标签#id >#id ; 标签.class > .class CSS优先级权 ...

  5. Nginx反向代理多虚拟主机代理

    根据http://www.cnblogs.com/zzzhfo/p/6032095.html这个环境配置 在web01和web02上配置基于域名的虚拟主机 web01 [root@web01 /]# ...

  6. [Asp.net MVC]Asp.net MVC5系列——添加模型

    目录 概述 添加模型 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net MVC5系列——添加视图 概述 在本节中我们将追加 ...

  7. iOS跳转到另一个程序

    我这里只是写了部分东西,如果想看更加详细的,请点击原文链接. 原文链接:http://blog.csdn.net/likendsl/article/details/7553605   原则上iOS的沙 ...

  8. 使用json存储结构化数据

    从文件中读写字符串很容易.数值就要多费点儿周折,因为read ()方法只会返回字符串,应将其传入int()这样的函数,就可以将'123'这样的字符串转换为对应的数值 123.当你想要保存更为复杂的数据 ...

  9. SNMP协议入门

    SNMP协议入门 1.引言 基于TCP/IP的网络管理包含3个组成部分: 1) 一个管理信息库MIB(Management Information Base).管理信息库包含所有代理进程的所有可被查询 ...

  10. Sqli-LABS通关笔录-14

    这一节让我学习到了 1.extractvalue函数(该函数用于对xml文件进行查询和修改,于此相关的还有一个叫“updatexml”函数) 语法:extractvalue(XML_document, ...