070701

题目

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.

解题思路

我的第一解题思路:

  1. 先把输入的数组按位转换为整数;
  2. 整数+1后转为字符,然后把字符按位取出来转为int型后存入array res
  3. return res
class Solution:def plusOne(self, digits: List[int]) -> List[int]:num=0for i in range(len(digits)):num=num*10+digits[i]num+=1res = []for i in str(num):res.append(int(i))return res

看看大佬们的,看到了一个比较有趣的递归做法:

  1. 直接判断整数的最后一位是否为9,如果不是,直接最后一位+1;
  2. 否则,递归调用该函数,这里需要注意,如果输入是9,则递归调用时digits的长度为0,所以当digits的长度为0,令其为1。
class Solution:def plusOne(self, digits: List[int]) -> List[int]:if len(digits)== 0:digits=[1]   elif digits[-1] == 9:digits = self.plusOne(digits[:-1])digits.append(0)else:digits[-1] += 1return digits

LeetCode: 66. Plus One相关推荐

  1. 【细节处理】LeetCode 66. Plus One

    LeetCode 66. Plus One 博客转载自:http://www.cnblogs.com/grandyang/p/4079357.html Solution1: class Solutio ...

  2. ⭐算法入门⭐《线性枚举》简单09 —— LeetCode 66. 加一

    文章目录 一.题目 1.题目描述 2.基础框架 3.原题链接 二.解题报告 1.思路分析 2.时间复杂度 3.代码详解 三.本题小知识 四.加群须知 一.题目 1.题目描述   给定一个由 整数 组成 ...

  3. LeetCode 66. 加一

    1. 题目 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示例 ...

  4. leetcode#66. Plus One

    题目 Given a non-negative integer represented as a non-empty array of digits, plus one to the integer. ...

  5. Java实现 LeetCode 66 加一

    66. 加一 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示 ...

  6. leetcode 66. 加一(C语言)

    题目: 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示例 1 ...

  7. leetcode 66 Plus One

    给定一个数组,表示整数的各个位数,现要将其加上1,考虑进位. vector<int> plusOne(vector<int>& digits) {int size = ...

  8. LeetCode 66 加一

    原题链接 关键词 :数组  模拟法 解题思路: 此题为处理数组,数组特点  --数组下标  可以前后覆盖  数组元素可交换等. 此题分为三种情况 1.传入数为234形式   ,加一后无进位.直接在最后 ...

  9. LeetCode 66. Plus One

    题目: Given a non-negative integer represented as a non-empty array of digits, plus one to the integer ...

最新文章

  1. SmartDroid论文阅读
  2. 鸿蒙是安卓换皮UI,鸿蒙2.0是安卓换皮?还真不是
  3. java通过maven构建项目实现日志生成模拟(二)构建数据,FastJson方法的使用
  4. 【Python基础】5个Pandas技巧
  5. java ==号比较String字符串的地址
  6. Sublime text 入门学习资源篇及其基本使用方法
  7. 施一公“开学第一课”金句频出!这堂硬核公开课讲了啥?
  8. 篮球战术谈之1-2-2进攻法
  9. 运用“异或”对原文加密,并解密
  10. [日志]挂在树上的茶壶
  11. Android View layout方法的简单使用案例
  12. linux格式u盘没有fat32,U盘,移动硬盘安装Linux的主分区,逻辑分区,FAT32格式分区的问题...
  13. 服务器如何安装虚拟声卡,虚拟声卡驱动VirtualAudioCable安装使用设置教程
  14. 周报、月报有多折磨人?万能报表模板建议收藏!(附模板)
  15. 在 WindowsXP运行UCDOS (天空之翔)
  16. 卸载计算机安全证书,卸载https证书详细的步骤有哪些
  17. 怎样用微信与电脑连接服务器,怎么使用WebSocket进行微信小程序远程控制电脑屏幕?...
  18. Windows 10新版可以更新了!这些新功能值得升级
  19. python实用性函数分享_17.【Python学习分享文章】function(函数)1
  20. 进阶之路(中级篇) - 015 串口控RGB三色灯

热门文章

  1. springBoot 搭建web项目(前后端分离,附项目源代码地址)
  2. php创建多级目录完整封装类操作
  3. C#winform使用+=和-=订阅事件和移除事件订阅
  4. 转-Redis学习手册(目录)
  5. pytorch Bert 情感分类
  6. Python 字典(Dictionary) copy()方法
  7. Springcloud Feign原理
  8. python codecs模块
  9. python axis=0 axis=1的区别
  10. pyplot.plot画图turtouil