Given a non-empty array of digits representing a non-negative integer, plus one to the integer.The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.You may assume the integer does not contain any leading zero, except the number 0 itself.

Example 1:

Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.

Example 2:

Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321. 思路

  这道题在python中还是挺好解决的, 直接从列表的尾部开始进行并设置一个溢出标志量,然后执行加法操作,如果加之后的结果小于10的话,直接中断,并判断溢出标志量。否则指针减1,开始新一轮的计算。直到遍历到头部结束。时间复杂度为O(n), 空间复杂度为O(1)。
解决代码


 class Solution(object):
def plusOne(self, nums):
"""
:type digits: List[int]
:rtype: List[int]
"""
index = len(nums)-1
if not nums:
return []
flow = 0 # 溢出标志量
while index >= 0: # 从尾部开始向前遍历
tem = nums[index] + 1
if tem >= 10: # 如果结果大于等于10的话,设置溢出标志量
flow = 1
nums[index] = tem %10
else: # 否则直接中断
nums[index] = tem %10
flow = 0
break
index -= 1
if flow == 1: # 最后判断溢出是否为1, 因此可能会遇到比如999这种输入。
nums.insert(0, 1) # 在头部插入
return nums

【LeetCode每天一题】Plus One(加一)的更多相关文章

  1. leetcode第37题--Count and Say

    题目:(据说是facebook的面试题哦) The count-and-say sequence is the sequence of integers beginning as follows:1, ...

  2. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  3. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  4. leetcode 入门第一题 4ms? 8ms? Two Sum

    今天开启leetcode 入门第一题 题意很简单,就是一个数组中求取两数之和等于目标数的一对儿下标 1.暴力 n^2 两个for循环遍历 用时0.1s 开外 代码就不用写了 2.二分 nlogn 我们 ...

  5. LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  6. Leetcode 第 2 题(Add Two Numbers)

    Leetcode 第 2 题(Add Two Numbers) 题目例如以下: Question You are given two linked lists representing two non ...

  7. 【JavaScript】Leetcode每日一题-平方数之和

    [JavaScript]Leetcode每日一题-平方数之和 [题目描述] 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c . 示例1: 输入:c = 5 ...

  8. [LeetCode每日一题]81. 搜索旋转排序数组 II

    [LeetCode每日一题]81. 搜索旋转排序数组 II 问题 已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同. 在传递给函数之前,nums 在预先未知的某个下标 k(0 & ...

  9. 【python】Leetcode每日一题-笨阶乘

    [python]Leetcode每日一题-笨阶乘 [题目描述] 通常,正整数 n 的阶乘是所有小于或等于 n 的正整数的乘积.例如,factorial(10) = 10 * 9 * 8 * 7 * 6 ...

  10. [LeetCode每日一题]781. 森林中的兔子

    [LeetCode每日一题]781. 森林中的兔子 问题 森林中,每个兔子都有颜色.其中一些兔子(可能是全部)告诉你还有多少其他的兔子和自己有相同的颜色.我们将这些回答放在 answers 数组里. ...

随机推荐

  1. matplotlib注解-【老鱼学matplotlib】

    本节讲述在图片中添加注解. 直接上代码: import numpy as np import pandas as pd import matplotlib.pyplot as plt # 生成x轴上的 ...

  2. chattr和lsattr的基本用法

    lsattr filename  查看一个文件的属性 chattr filename  改变一个文件的属性 主要用途:实现文件的原有内容不允许改变,但可以增加新行,则需通过改变文件的属性来实现. ch ...

  3. 面试题:常用的http状态码

    3XX 重定向 301 Moved Permanently    永久重定向,表示请求的资源已经永久的搬到了其他位置 302 Found  临时重定向,表示请求的资源临时搬到了其他位置 303 See ...

  4. SQLyog 最新版本12.5-64bit 完美破解,亲测可用!

    声明:本文只是提供一个网络上找到的针对12.5版本的注册码使用方式做一个说明,不建议企业用户破解,有条件的还是希望大家购买原版.当然个人学习用的但又不想购买原版的,这里只是提供个途径,请勿用做商业用途 ...

  5. HttpHandler和ashx使用Session 出现未初始化异常

    原因: HttpHandler和ashx要实现IRequiresSessionState接口才能访问Session信息 接口IRequiresSessionState: 指定目标 HTTP 处理程序需 ...

  6. JSON WEB Token(JWT)

    最近面试被问及单点登陆怎么解决?自己的项目前后端分离,自己实现token认证,token有失效时间,token中包含用户基本的信息.且一个当用户重新登陆后,原来的token就会失效,这么安全的一个to ...

  7. Manacher算法学习笔记 | LeetCode#5

    Manacher算法学习笔记 DECLARATION 引用来源:https://www.cnblogs.com/grandyang/p/4475985.html CONTENT 用途:寻找一个字符串的 ...

  8. myeclipse使用git图文教程

    Git介绍与使用 1.什么是Git Git是分布式版本控制系统 Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. 2.集中式版本控制系统(CVS / SVN等) 集中 ...

  9. MySQL数据库的几种引擎

    有些东西其实一直在用,但是突然问起来它是啥,可能你会很陌生,很陌生,很陌生 ....... mysql的四种引擎: 1.MyISAM存储引擎 不支持事务,不支持外键,优势是访问速度快,对事务完整性没有 ...

  10. chrome浏览器另存为/上传附件崩溃

    x 前言 系统中有一个需要上传导入的功能,此功能在谷歌浏览器上传总是直接崩溃, 但是在火狐浏览器中就是好的. 自己想到的解决方案 谷歌浏览器版本号得问题?自己系统得问题? 谷歌浏览器卸载重新安装,还是 ...