1.题目描述

Given a number represented as an array of digits, plus one to the number.

2.解法分析

不要被常规思路限制住,常规思路会遍历整个数组,然后设置进位,但是实际上只需要找到第一位不为9的数字即可,免去了加的必要。

class Solution {

public:

    vector<int> plusOne(vector<int> &digits) {

        // Start typing your C/C++ solution below

        // DO NOT write int main() function

        if(digits.empty())return digits;

        int len=digits.size();

        

        int i=len-1;

        while(i>=0)

        {

            if(digits[i]==9)digits[i]=0;

            else{

                digits[i]+=1;break;

            }

            i--;

        }

        

        if(i<0)digits.insert(digits.begin(),1);

        

        return digits;

        

    }

};

leetcode—Plus one的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  10. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. 2014--9=17 软工二班 MyEclipse blue==4

    package cn.rwkj.test; import java.io.IOException; import java.io.InputStream; import java.io.OutputS ...

  2. oracle SQL Develop导出数据库中的表格数据到excel

    首先打开oracle数据库 1.查询数据库, SELECT * FROM pub_attribute WHERE ELEMENT_CODE='bb382e10d7ce437b8a8c980ba20ac ...

  3. Android Studio上的几个插件

    转载:http://blog.csdn.net/maosidiaoxian/article/details/44992655 以下所有插件都可以在Idea的插件库中找到,如果你与我一样在Android ...

  4. android动画坐标定义

    这段时间一直在忙Android的项目,总算抽出点时间休息一下,准备把一些项目用到的Android经验分享一下. 在Android开发过程中,经常会碰到Activity之间的切换效果的问题,下面介绍一下 ...

  5. HDU 1698 Just a Hook (线段树 成段更新 lazy-tag思想)

    题目链接 题意: n个挂钩,q次询问,每个挂钩可能的值为1 2 3,  初始值为1,每次询问 把从x到Y区间内的值改变为z.求最后的总的值. 分析:用val记录这一个区间的值,val == -1表示这 ...

  6. asp.net正则表达式过滤标签和数据提取

    无论什么语言,正则表达式的处理方法都是非常灵活.高效的,尤其是对某些字符串的抓取.过滤方面,更显其优势. 正则表达式的写法通常比较简单,几行短代码便能轻松完成看似很复杂的事情,更值得称赞的是,它的执行 ...

  7. noip2007提高组题解

    题外话:这一年的noip应该是最受大众关心的,以至于在百度上输入noip第三个关键字就是noip2007.主要是由于这篇文章:http://www.zhihu.com/question/2110727 ...

  8. LeetCode: Sqrt

    Title: Implement int sqrt(int x). Compute and return the square root of x. 思路:这个平方根肯定是在[1,x]之间,所以在这个 ...

  9. RTP协议学习大总结从原理到代码

    from:http://wenku.baidu.com/view/aaad3d136edb6f1aff001fa5.html 一.流媒体概念 流媒体包含广义和狭义两种内涵:广义上的流媒体指的是使音频和 ...

  10. vim变ide

    如果你稍微写过一点代码,就能知道“集成开发环境”(IDE)是多么的便利.不管是Java.C还是Python,当IDE会帮你检查语法.后台编译,或者自动导入你需要的库时,写代码就变得容易许多.另外,如果 ...