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. HTML5 增强的页面元素

    一.HTML5 改良的 input 元素的种类 1.<input type="number" id="num1"> var n1 = documen ...

  2. Google不做坏事吗?

    说中国足球为什么冲不出亚洲,那是因为咱中国人太文气,足球是种“斗牛士”式的游戏,得玩的有点儿“野蛮”色彩.记得以前在英国的时候,遇上联赛,晚上大街小巷全民皆兵,曼切斯特队的粉丝在街道一边酒吧里,利物浦 ...

  3. python list去重的方法

    转载于:http://yxmhero1989.blog.163.com/blog/static/112157956201381443244790/ Python很简洁 我们喜欢简单有效的代码   一. ...

  4. BIND9配置文件详解模板[转载]

    在CU上看到了一篇关于BIND9配置文件详解的文章,感觉不错,现转载了分享一下. //named.conf 注释说明 by shellyxz@163.com// 此文件对bind9的默认配置文件的说明 ...

  5. Collection_Other

    package com.bjsxt.others.que; import java.util.ArrayDeque; import java.util.Queue; /** * 使用队列模拟银行存款业 ...

  6. C# Index 定义索---引具体使用2

    窗体代码 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;usi ...

  7. fil_space_create

    /*******************************************************************//** Creates a space memory obje ...

  8. ASP.NET MVC Html.ActionLink使用说明

    本文整理了该方法的几种重载形式: 1.Html.ActionLink("linkText","actionName")该重载的第一个参数是该链接要显示的文字,第 ...

  9. asp.net创建XML文件方法

    方法一:按照XML的结构一步一步的构建XML文档.    通过.Net FrameWork SDK中的命名空间"System.Xml"中封装的各种类来实现的 方法一:按照XML的结 ...

  10. java中的final、finally和finalize

    最近在读Thinking In Java,秉着有些地方还能知道自己不会的精神,都去好好查阅了一些资料,在内存分配这一章,看到finalize()这个方法,刚开始很不理解,查阅了一些资料,顺带看了一下f ...