题目

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. fancyBox高级进阶用法

    最近给客户做的一个项目中,客户要求弹窗的边界与页面某个区块边界平齐,但平齐之后,弹出的窗口就不是居中的情况了,研究之后,认为需要改写fancyBox的fancybox-wrap类中的top属性才能达到 ...

  2. touch事件总结

    $("body").on("touchstart", function(e) { e.preventDefault(); startX = e.original ...

  3. python 实例方法,类方法,静态方法

    实例方法 class Human(object): def __init__(self, weight): self.weight = weight def get_weight(self): ret ...

  4. Hyper-V 2016 配置管理系列(应用篇)

    远程连接到Hyper-V HOST 为了日常运维管理操作,使用远程PowerShell工作.Windows 10上安装了RSAT(远程管理工具 ).然后安装了Hyper-V控制台: 在能够远程连接到H ...

  5. MVC下c#对接微信公众平台开发者模式

    在ashx文件中进行HttpContext的处理: using System; using System.Collections.Generic; using System.Linq; using S ...

  6. HDU1664 BFS + 数论 + 剪枝

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1664 , 一道比较蛋疼的搜索题. 这道题有很多坑点,一点处理不好就要TLE. 题意很简单,就是找到一个 ...

  7. SwiftHN阅读器应用IOS源码

    SwiftHN是用Swift语言编写的Hacker News阅读器,同时采用了iOS 8最新的API. <ignore_js_op> <ignore_js_op> 详细说明:h ...

  8. 【luogu P3608 [USACO17JAN]Balanced Photo平衡的照片】 题解

    题目链接:https://www.luogu.org/problemnew/show/P3608 乍一看很容易想到O(N^2)的暴力. 对于每个H[i]从i~i-1找L[i]再从i+1~n找R[i], ...

  9. c++中的结构化语句 判断语句if 分支语句switch 循环语句 while 和 do while 循环语句for的使用

    作业1: 使用if语句,根据1~7的数字,输出今天是星期几?的程序. 方法一,直接使用单独的if语句 #include <iostream> using namespace std; in ...

  10. Java - 通过私有构造方法获取实例