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. Scrapy Selectors 选择器

    0. 1.参考 <用Python写网络爬虫>——2.2 三种网页抓取方法  re / lxml / BeautifulSoup 需要注意的是,lxml在内部实现中,实际上是将CSS选择器转 ...

  2. 在XPS13 上安装Ubuntu 16.04

    1 准备系统安装U盘 使用常见的光盘工具软件ultraISO. (1)首先使用UltraISO打开Ubuntu-16.04.4-desktop-amd64.iso安装映像. (2)在菜单栏中,选择&q ...

  3. pc端网页,移动端网页(andorid、ios)离开页面做一个时间存储

    如图所示:在一个页面中做了一个倒计时,然后用户想离开页面做其他事情,需求是离开页面之后把时间保存,下一次进来继续的时候时间还是上次离开的时间 第一次我用的事件是:  // window.onbefor ...

  4. Windows上为Apache配置HTTPS

    Windows上为Apache配置HTTPS   转 https://www.cnblogs.com/tianzijiaozi/p/7582671.html   1. 安装OpenSSL: Windo ...

  5. angular.isString()

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. MySql数据and高级查询

    1.CREATE TABLE grade(stuID INT AUTO_INCREMENT PRIMARY KEY NOT NULL,stuName VARCHAR(32),stuAge INT ) ...

  7. vue2.0无限滚动加载数据插件

      做vue项目用到下拉滚动加载数据功能,由于选的UI库(element)没有这个组件,就用Vue-infinite-loading 这个插件代替,使用中遇到的一些问题及使用方法,总结作记录! 安装: ...

  8. Hangover POJ - 1003

    How far can you make a stack of cards overhang a table? If you have one card, you can create a maxim ...

  9. C. Vasya and Robot二分

    1.题目描述 Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell ...

  10. SSIS获得Excel行号(转自http://blog.csdn.net/zplume/article/details/19113911)

    问题描述: 首先个人并不推荐将Excel作为数据源,因为Excel单元格式会引起特别多的数据转换问题,例如:单元格里明明是2.89,但SSIS抽取到数据库里面之后却变成了2.88999999之类的数据 ...