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. Week 3: Structured Types 5. Tuples and Lists Exercise: odd tuples

    Exercise: odd tuples 5/5 points (graded) ESTIMATED TIME TO COMPLETE: 5 minutes Write a procedure cal ...

  2. 线性递推规律BM杜教

    #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> # ...

  3. HDU6393(LCA + RMQ + 树状数组) n边图,两点最短距离 , 修改边权

    这道题的进阶版本 进阶版本 题意: 一个n个点,n条边的图,2中操作,1是将某条边的权值更改,2是询问两点的最短距离. 题解: 由于n个点,n条边,所以是树加一个环,将环上的边随意取出一条,就是1颗树 ...

  4. PIE SDK自定义滤波

    1.算法功能简介 自定义滤波可以自由设置滤波模板,对数据进行处理,自定义滤波器的一般规则要求: ( 1) 滤波器的大小应该是奇数,这样它才有一个中心,例如 3x3, 5x5 或者 7x7.有中心了,也 ...

  5. 常用sql语句整理[MySql]

    查看执行计划 explain update test100 set contractSn=99 where contractSn=45; insert ... on duplicate key使用 i ...

  6. Linux下安装配置MongoDB数据库

    说明: 操作系统:CentOS 5.X 64位 IP地址:192.168.21.130 实现目的: 安装配置MongoDB数据库 具体操作: 一.关闭SElinux.配置防火墙 1.vi /etc/s ...

  7. React.js 小书 Lesson8 - 组件的组合、嵌套和组件树

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson8 转载请注明出处,保留原文链接和作者信息. 继续拓展前面的例子,现在我们已经有了 Heade ...

  8. bzoj 5340: [Ctsc2018]假面

    Description 题面 Solution 生命值范围比较小,首先维护每一个人在每个血量的概率,从而算出生存的概率,设为 \(a[i]\) 询问时,只需要考虑生存的人数,可以 \(DP\) 设 \ ...

  9. 一个不错的架构图:基于SpringCloud的微服务项目

    https://github.com/hanlin5566/HJ-MicroService

  10. java实现截取6个汉字字母数字

    项目中使用到需要截取6个字(12个字母或数字),解决方法如下: /** * * @方法名称:getWordCount * @内容摘要: <截取输入字符串 大于6个后显示...> * @pa ...