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. SQLite数据插入异常

    对比两条SQL语句 1.insert into MemberInfo(MTypeId,MName,MPhone,MMoney,MIsDelete) values(@tid,@name,@phone,@ ...

  2. Android 类似时间轴的实现

    想要实现图片中的的时间轴的效果,设定了三种颜色,但是出来的只有一个黑色,还不是设定好的,而且长度很长的话不能滚动,下面上代码: 布局文件: <LinearLayout xmlns:android ...

  3. highcharts 结合phantomjs纯后台生成图片系列二之php2

    上篇文章中介绍了phantomjs的使用场景,方法. 本篇文章详细介绍使用php,highcharts 结合phantomjs纯后台生成图片.包含一步步详细的php代码 一.highcharts 结合 ...

  4. 7、单向一对多的关联关系(1的一方有n的一方的集合属性,n的一方却没有1的一方的引用)

    单向一对多的关联关系 具体体现:1的一方有n的一方的集合的引用,n的一方却没有1的一方的引用 举个例子:顾客Customer对订单Order是一个单向一对多的关联关系.Customer一方有对Orde ...

  5. 基于条件随机场(CRF)的命名实体识别

    很久前做过一个命名实体识别的模块,现在有时间,记录一下. 一.要识别的对象 人名.地名.机构名 二.主要方法 1.使用CRF模型进行识别(识别对象都是最基础的序列,所以使用了好评率较高的序列识别算法C ...

  6. 2008年我买了一本书 书名叫“PHP 6”

    上个星期天,我感觉应该整理一下我的书柜.于是,在书柜里,我发现了一本几乎完全忘记的书(我想不起来为什么要买它!):<PHP 6 – 快速简洁的Web开发> 这本书出版于2008年一月.而六 ...

  7. nyoj-291 互素数个数 欧拉函数

    LK的数学题 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 LK最近遇到一个问题,需要你帮她一下.一个整数n,求[1,n)中,和n互素的数的个数.   输入 多组测 ...

  8. C#获取ip的示例

    界面 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using ...

  9. C#中控件的CheckState和Checked属性区别?

    Checked 和CheckState都是检查控件选中状态,都能判断是否选中控件. 只是Checked 通过布尔判断(true & false): CheckState 通过枚举判断. che ...

  10. CFF前端沙龙总结

    一. -OOCSS + Sass ——大漠 1. OOCSS 结构<=>皮肤 分离 容器<=>内容 分离 2. Sass 工具.处理器 SCSS(CSS风格)<=> ...