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.

解题方法1:

模拟运算过程,从低位数字到高位数字计算,新建一个变量记录进位情况。

 class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
int len = digits.size();
int sig = ;
digits[len-] += ;
for (int i=len-; i>=; --i) {
digits[i] += sig;
sig = digits[i] / ;
digits[i] = digits[i] % ;
} if (sig == ) {
digits.insert(digits.begin(), );
} return digits;
}
};

更聪明的方法(应该是最棒的方法):

1、计算过程其实问题就是两步,从最低位开始,进行如下循环:如果不是9,则此位++,break;如果是9,则此位为0,continue;

2、当最高位满10需要进位时:最高位满10只有一种情况,即99..99+1,所以在第一条方法基础上,计算后判断首位(最高位)是否是0,是0则表示最高位发生了进位。将最高位置1,最低位补充一位0;

3、更多的细节:如果在1过程中遇到了某位不是9,则不必在break后判断首位是不是0,因此可以直接返回;

           既然如此,只要程序在1过程中没有返回,而运行到了循环外,就说明首位发生了进位,可以不用判断,直接进行步骤2。

具体代码:

 class Solution {
public:
vector<int> plusOne(vector<int> &digits)
{
int n = digits.size();
for (int i=n-; i>=; --i)
{
if (digits[i] == ) {
digits[i] = ;
} else {
digits[i]++;
return digits;
}
} digits[] =;
digits.push_back(); return digits;
}
};

附录:

vector、list等特性及使用注意。

【Leetcode】【Easy】Plus One的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  6. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

  7. 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number

    [Q7]  把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...

  8. 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters

    [Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...

  9. 【LeetCode算法题库】Day5:Roman to Integer & Longest Common Prefix & 3Sum

    [Q13] Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Valu ...

  10. 【LeetCode题意分析&解答】38. Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

随机推荐

  1. 误删除google浏览器用户,如何恢复之前收藏的标签,亲测有效

    第一种方案: 看 路径 C:\Users\admin\AppData\Local\Google\Chrome\User Data\Default  下或者 C:\Users\admin\AppData ...

  2. Linux acpi off学习的必要

    ACPI是Intel(i386,x86_64,IA64)平台的标准固件规范,绝大部分OS需要从BIOS得到的信息都可以从ACPI得到,并且现在的趋势是未来的任何新的特性相关的信息都只能从ACPI得到. ...

  3. Oracle使用sys登录时报错ORA-28009解决方法

    情况一:使用sqlplus登录: 正常输入用户名的口令,就会报错,因为SYS是在数据库之外的超级管理员,所以我们在登录的时候 要在输入口令:口令+as sysdba(比如:123456 as sysd ...

  4. Mixamo Fuse10分钟创建角色

    http://edu.manew.com/course/132 太6了

  5. xss攻击汇总--转

    (1)普通的XSS JavaScript注入<SCRIPT SRC=http://3w.org/XSS/xss.js></SCRIPT>(2)IMG标签XSS使用JavaScr ...

  6. ebiao 报表工具使用入门

    一.ebiao简价 e表是一个功能强大的Web报表工具,可使复杂报表的设计简单化,避免了大量的复杂SQL编写以及编程来准备数据,报表设计的效率大大提高.e表分为e表 for .NET和e表 for J ...

  7. ztree使用方法 python后台

    一.在提前加载js的地方写一段js,判断该页面是否需要添加ztree,我的项目所有提前加载的js都写在admin.js中 //增加ztree $(document).ready(function() ...

  8. [转]解读ASP.NET 5 & MVC6系列(8):Session与Caching

    本文转自:http://www.cnblogs.com/TomXu/p/4496445.html 在之前的版本中,Session存在于System.Web中,新版ASP.NET 5中由于不在依赖于Sy ...

  9. UiPath进阶

    最近RPA比较火,UiPath工具排名前几位并且免费试用,很多朋友们都选择了学习自动化工具UiPath,今天我就向大家介绍一下UiPath的学习过程,希望对后来的学习这个工具的人有所帮助. UiPat ...

  10. c#-cs-bs-正则表达式

    C/S     B/S Cs结构:     C/S(Client/Server)客户机/服务器 BS机构:     B/S(Browser/Server)浏览器/服务器       à(未来发展方向) ...