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. CSS之垂直对齐

    vertical-align: baseline 默认.元素放置在父元素的基线上. sub 垂直对齐文本的下标. super 垂直对齐文本的上标 top 把元素的顶端与行中最高元素的顶端对齐 text ...

  2. PHP 闭包函数

    PHP>v5.3闭包函数,闭包函数没有函数名称,直接在function()传入变量即可 使用时将定义的变量当作函数来处理 匿名函数也叫闭包函数(closures允许创建一个没有指定没成的函数,最 ...

  3. .net core 设置访问静态资源

  4. stm32位操作详解

    stm32位操作详解 STM32位操作原理 思想:把一个比特分成32位,每位都分配一个地址,这样就有32个地址,通过地址直接访问. 位操作基础 位运算 位运算的运算分量只能是整型或字符型数据,位运算把 ...

  5. C#---ASP页面的下拉框模糊查询功能

    基础方法支持: 一. DataTable 转换成 Json 换句话说如何在ASP.NET将一个DataTable序列化为 Json数组,或者如何从一个DataTable返回一个Json字符串. 使用 ...

  6. css-tips

    div的height:100%有作用 其父元素设置height:100% 包括html,body

  7. [JAVA] TicTacToe实现Socket通信(一)

    先来两张预览,大家可以试试jar包了,有什么问题评论哈,过两天贴代码 jar包这里下载 https://github.com/Andy-ZYA/TicTacToe_JAVA_Socket_Swing

  8. Server response error code:404, error:{"ret":-1, "msg":"invalid appkey"}

    Server response error code:404, error:{"ret":-1, "msg":"invalid appkey" ...

  9. TSL 访问器

    设计原理:GE有一个分布式内存基础设施,成为内存云.内存云由一组内存主干组成.集群中的每台机器承载256个内存中继.我们将一台机器的本地内存空间划分为多个内存中继的原因有两方面:1)中继级别的并行性可 ...

  10. JAVA RPC (三) 之thrift序列化协议入门杂谈

    首先抱歉让大家久等了,最近工作的原因,再加上自己维护koalas rpc利用的大部分时间,一直没腾出空来写这篇文章. 先放出来自研的企业级RPC框架源代码地址,上面有使用方式和部署环境说明,说环境部署 ...