题目

Given a non-negative number represented as an array of digits, plus one to the number.

The digits are stored such that the most significant digit is at the head of the list.

代码:oj测试通过 Runtime: 47 ms

 class Solution:
# @param digits, a list of integer digits
# @return a list of integer digits
def plusOne(self, digits):
if digits is None:
return None
if len(digits) == 0 :
return digits
length = len(digits)
new_last_digit = digits[length-1] + 1
if new_last_digit / 10 == 0:
digits[length-1] = new_last_digit
return digits
else:
jinwei = 1
digits[length-1] = 0
for i in range(length-2,-1,-1):
curr_value = jinwei + digits[i]
if curr_value/10 == 0:
digits[i] = curr_value
return digits
else:
digits[i] = curr_value % 10
digits.insert(0,1)
return digits

思路

先排除special case

然后就逐渐往前加,维护一个jinwei变量

注意 如果最后有进位 需要在数组开头再补充一个1 这里用到了insert函数 之前还不知道python数组有这个函数

leetcode 【 Plus One 】python 实现的更多相关文章

  1. [LeetCode]题解(python):125 Valid Palindrome

    题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...

  2. [LeetCode]题解(python):120 Triangle

    题目来源 https://leetcode.com/problems/triangle/ Given a triangle, find the minimum path sum from top to ...

  3. [LeetCode]题解(python):119 Pascal's Triangle II

    题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...

  4. [LeetCode]题解(python):118 Pascal's Triangle

    题目来源 https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numRows of Pa ...

  5. [LeetCode]题解(python):116 Populating Next Right Pointers in Each Node

    题目来源 https://leetcode.com/problems/populating-next-right-pointers-in-each-node/ Given a binary tree ...

  6. [LeetCode]题解(python):114 Flatten Binary Tree to Linked List

    题目来源 https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ Given a binary tree, flatten ...

  7. [LeetCode]题解(python):113 Path Sum II

    题目来源 https://leetcode.com/problems/path-sum-ii/ Given a binary tree and a sum, find all root-to-leaf ...

  8. [LeetCode]题解(python):112 Path Sum

    题目来源 https://leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if the tree ha ...

  9. [LeetCode]题解(python):111 Minimum Depth of Binary Tree

    题目来源 https://leetcode.com/problems/minimum-depth-of-binary-tree/ Given a binary tree, find its minim ...

  10. [LeetCode]题解(python):110 Balanced Binary Tree

    题目来源 https://leetcode.com/problems/balanced-binary-tree/ Given a binary tree, determine if it is hei ...

随机推荐

  1. 使用HTML5 canvas做地图(1)基础知识

    之前一直想使用HTML5技术全新做一套地图API,可是苦于时间和精力,迟迟未有行动.后来下定决心,利用下班和周末做出一个大体框架出来,现在和网友分享一下自己的整体的一个思路和想法.欢迎大家提出宝贵建议 ...

  2. 构建第一个spring boot2.0应用之项目启动运行的几种方式(二)

    方法一. 配置Run/Debug Configuration  选择Main Class为项目 Application启动类(入口main方法) (2).进行项目目录,即包含pom.xml的目录下,启 ...

  3. php-7.1.11编译选项配置

    ./configure \ --prefix=/usr/local/php-7.1.11 \ --with-config-file-path=/usr/local/php7.1.11/etc \ -- ...

  4. Linux常用命令-1

    内部命令:属于Shell解释器的一部分(已调入内存) 外部命令:独立于Shell解释器之外的程序文件(在磁盘上) 获得命令帮助 1)内部命令help 查看Bash内部命令的帮助信息 2)命令的“--h ...

  5. 上传高德地图-express框架

    1.首先要注册高德地图,完后成为开发者 2.控制台里获取自己的key值 3.在要显示地图的页面添加如下的代码 <script type="text/javascript" s ...

  6. HDU 2191 悼念汶川地震(多重背包)

    思路: 多重背包转成01背包,怎么转?把一种大米看成一堆单个的物品,每件物品要么装入,要么不装.复杂度比01背包要大.时间复杂度为O(vns)(这里S是所有物品的数量s之和).这个做法太粗糙了,但就是 ...

  7. python_11_guess任性玩

    age_of_oldboy=56 count=0 while count<3: guess_age=int(input("guess age:")) if guess_age ...

  8. Python实现进度条小程序

    一.print()参数介绍 1.end:指定打印结束后添加的字符,默认值为换行符 for j in range(3): print('hello world') for i in range(3): ...

  9. 使用Scanner类获取键盘输入的会员卡号,并将该数据存储在变量中,输出这个变量的信息

    package come.one01;import java.util.Scanner; // 导入Scanner类public class One03 { public static void ma ...

  10. idea右侧的工具栏不见,包括maven以及其他

    通用,设置后右侧工具栏可见 可以通过view-->windows tool看到maven的