1.编辑器

我使用的是win10+vscode+leetcode+python3
环境配置参见我的博客:
链接

2.第六十六题

(1)题目
英文:
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.

中文:
给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。

最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。

你可以假设除了整数 0 之外,这个整数不会以零开头。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/plus-one

(2)解法
① 直接法(耗时:32ms,内存:13.7M)

class Solution:def plusOne(self, digits: List[int]) -> List[int]:digits[len(digits)-1]+=1for indx in range(len(digits)-1, -1, -1):if indx!=0:if digits[indx]>=10:  # 其实,这里可以直接写成==10,因为加1进位肯定是原来位是9。digits[indx]-=10digits[indx-1]+=1else:if digits[indx]>=10:digits[indx]-=10digits.insert(0,1)return digits

注意:
1.这个是按照习惯从后面往前面加的方法,其实有点不方便,可以在最前面翻转一下digits: `digits[::-1]。
2.这个方法的关键是关注最高位indx=0的进位情况,如果还有进位,就必须在减10的基础上,在0位前插入1以表示最高位进位。

② 递归法(耗时:36ms,内存:13.8M)

class Solution:def plusOne(self, digits: List[int]) -> List[int]:def addOne(digits, indx):if indx == -1:digits.insert(0, 1)return digitsdigits[indx] += 1if digits[indx]==10:digits[indx] = 0return addOne(digits, indx-1)return digitsreturn addOne(digits, len(digits)-1)

注意:
1.通过与直接法比较,其实可以看出,直接法中已经有了递归的感觉了。
2.递归的三要素:
① 终止条件:
当最高位还有进位时,会在最高位插入1进位,然后输出digits
② 问题的规模要减少:
addOne(digits, indx-1)这个就是在一次一次的减少问题规模
③ 调用相同的函数:
addOne

leetcode python3 简单题66. Plus One相关推荐

  1. leetcode python3 简单题225. Implement Stack using Queues

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百二十五题 (1)题目 英文: Implement the following ...

  2. leetcode python3 简单题70. Climbing Stairs

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第七十题 (1)题目 英文: You are climbing a stair ca ...

  3. leetcode python3 简单题69. Sqrt(x)

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第六十九题 (1)题目 英文: Implement int sqrt(int x). ...

  4. leetcode python3 简单题53. Maximum Subarray

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第五十三题 (1)题目 英文: Given an integer array num ...

  5. leetcode python3 简单题58. Length of Last Word

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第五十八题 (1)题目 英文: Given a string s consists ...

  6. leetcode python3 简单题14. Longest Common Prefix

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第十四题 (1)题目 英文: Write a function to find th ...

  7. leetcode python3 简单题234. Palindrome Linked List

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百三十四题 (1)题目 英文: Given a singly linked li ...

  8. leetcode python3 简单题232. Implement Queue using Stacks

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百三十二题 (1)题目 英文: Implement the following ...

  9. leetcode python3 简单题231. Power of Two

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百三十一题 (1)题目 英文: Given an integer, write ...

最新文章

  1. Spring3, Hibernate3.6与Proxool连接池配置
  2. android控制小米设备吗,智能设备一指连:小米 UWB 技术发布,手机指向设备直接操控...
  3. Gentoo 安装日记 05 (磁盘分区)
  4. python下载opencv库_Window系统下Python如何安装OpenCV库
  5. boost::mp11::mp_partial_sum相关用法的测试程序
  6. python观察日志(part15)--命名元祖namedtuple
  7. vxworks的default boot line说明
  8. Linux搜狗输入法候选词乱码
  9. linux共享内存 dest,关于linux 共享内存查看已经完整释放
  10. 有向图强连通分支的Tarjan算法讲解 + HDU 1269 连通图 Tarjan 结题报告
  11. 强化学习读书笔记 - 03 - 有限马尔科夫决策过程
  12. Tomcat starup.bat脚本开机自启动
  13. 排队论模型(六):非生灭过程排队模型、爱尔朗(Erlang)排队模型
  14. 在网站中使用VideoJs视频播放器播放视频
  15. Python自动化办公:读取Excel数据并批量生成合同
  16. 打印机脱机了的修复方法
  17. 从fit文件中提取lick指数的matlab程序
  18. NPAPI 为什么会被 Chrome 禁用
  19. 蚂蚁金服副 CTO胡喜:从 BASIC 到 basic ,蚂蚁金服十五年技术架构演进之路
  20. CVPR--2019 AI CITY CHALLENGE (track1成绩A榜第一,综合第二)

热门文章

  1. Python之 if-else
  2. 冒泡排序(【CCF】NOI Online能力测试 提高组第二题)
  3. 《Win测试的学习笔记》——Introduction
  4. 手把手教程 Sublime Text 删除重复行
  5. R语言正则表达式[stringr package]
  6. 为什么 npm 要为每个项目单独安装一遍 node_modules?
  7. Java中更换Map中的主键key的名称
  8. Java中List判空问题白话详解
  9. 【Qt教程】3.2 - Qt5 event事件、定时器timerEvent
  10. 【Spring Cloud】保护机制-Hystrix