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. 最完整的dos命令字典,IIS服务命令,FTP命令

    https://www.cnblogs.com/accumulater/p/10670051.html(优秀博文) 一.最完整的dos命令字典net use ipipc$ " " ...

  2. pandas导入导出数据-【老鱼学pandas】

    pandas可以读写如下格式的数据类型: 具体详见:http://pandas.pydata.org/pandas-docs/version/0.20/io.html 读取csv文件 我们准备了一个c ...

  3. matplotlib坐标轴设置-【老鱼学matplotlib】

    我们可以对坐标轴进行设置,设置坐标轴的范围,设置坐标轴上的文字描述等. 基本用法 例如: import numpy as np import pandas as pd import matplotli ...

  4. sbadmin表单事件

    Form表单 自定义表单 <from action="" method="'><!---      这里可以用表单组件快速生成表单元素哦        ...

  5. POJ 1985 Cow Marathon (模板题)(树的直径)

    <题目链接> 题目大意: 给定一颗树,求出树的直径. 解题分析:树的直径模板题,以下程序分别用树形DP和两次BFS来求解. 树形DP: #include <cstdio> #i ...

  6. 牛顿二项式与 e 级数

    复习一下数学, 找一下回忆. 先是从二项式平方开始: 其实展开是这样的: 再看立方: 通过排列组合的方式标记, 于是: 通过数学归纳法可以拓展: 使用求和简写可得: e 级数 数学常数 e (The ...

  7. 11 安装已集成HA的树莓派镜像Hassbian

    2017-09-04 10:40:47 下载Hassbian镜像文件,浏览https://github.com/home-assistant/pi-gen/releases/tag/v1.23,查看最 ...

  8. Linux 上安装JDK

    JDK下载 下载 JDK Linux 版本(注意看自己安装 Linux 系统的位数,本人的是64位) 1.oracle 官网下载地址:jdk-7u80-linux-x64.gz(可能需要注册下载,嫌麻 ...

  9. [LeetCode] Most Profit Assigning Work 安排最大利润的工作

    We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith ...

  10. myeclipse使用git图文教程

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