题目如下:

For a non-negative integer X, the array-form of X is an array of its digits in left to right order.  For example, if X = 1231, then the array form is [1,2,3,1].

Given the array-form A of a non-negative integer X, return the array-form of the integer X+K.

Example 1:

  1. Input: A = [1,2,0,0], K = 34
  2. Output: [1,2,3,4]
  3. Explanation: 1200 + 34 = 1234

Example 2:

  1. Input: A = [2,7,4], K = 181
  2. Output: [4,5,5]
  3. Explanation: 274 + 181 = 455

Example 3:

  1. Input: A = [2,1,5], K = 806
  2. Output: [1,0,2,1]
  3. Explanation: 215 + 806 = 1021

Example 4:

  1. Input: A = [9,9,9,9,9,9,9,9,9,9], K = 1
  2. Output: [1,0,0,0,0,0,0,0,0,0,0]
  3. Explanation: 9999999999 + 1 = 10000000000

Note:

  1. 1 <= A.length <= 10000
  2. 0 <= A[i] <= 9
  3. 0 <= K <= 10000
  4. If A.length > 1, then A[0] != 0

解题思路:题目很简单,但是在相加的过程中要注意进位。同时K > A的场景需要做特殊处理。

代码如下:

  1. class Solution(object):
  2. def addToArrayForm(self, A, K):
  3. """
  4. :type A: List[int]
  5. :type K: int
  6. :rtype: List[int]
  7. """
  8. carrier = 0
  9. for i in range(len(A)-1,-1,-1):
  10. remainder = K % 10
  11. K = K/10
  12. A[i] += (remainder + carrier)
  13. if A[i] >= 10:
  14. A[i] -= 10
  15. carrier = 1
  16. else:
  17. carrier = 0
  18. if carrier == 1 and K == 0:
  19. A.insert(0,1)
  20. elif K > 0:
  21. K += carrier
  22. while K > 0:
  23. A.insert(0, K% 10)
  24. carrier = 0
  25. K = K/10
  26. return A

【leetcode】989. Add to Array-Form of Integer的更多相关文章

  1. 【LeetCode】989. Add to Array-Form of Integer 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组转整数再转数组 模拟加法 日期 题目地址:htt ...

  2. 【LeetCode】623. Add One Row to Tree 解题报告(Python)

    [LeetCode]623. Add One Row to Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problem ...

  3. 【LeetCode】数组-6(561)-Array Partition I(比较抽象的题目)

    题目描述:两句话发人深思啊.... Given an array of 2n integers, your task is to group these integers into n pairs o ...

  4. 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...

  5. 【Leetcode_easy】989. Add to Array-Form of Integer

    problem 989. Add to Array-Form of Integer 参考 1. Leetcode_easy_989. Add to Array-Form of Integer; 完

  6. 【LeetCode】258. Add Digits (2 solutions)

    Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...

  7. 【LeetCode】 258. Add Digits 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:减1模9 方法三:直接模9 日 ...

  8. 【LeetCode】912. Sort an Array 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数排序 桶排序 红黑树排序 归并排序 快速排序 ...

  9. 【LeetCode】415. Add Strings 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...

随机推荐

  1. KEGG注释

    在 KEGG 数据库中,把功能相似的蛋白质归为同一组,然后标上 KO 号.通过相似性比对,可以为未知功能的蛋白序列注释上 KO 号. 截止到 2015 年 6 月 12 日,KEGG 数据库中共收录了 ...

  2. Linux网络栈

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11394930.html OSI模型 OSI 模型把网络互联的框架分为应用层.表示层.会话层.传输层.网 ...

  3. sql判断中文、数字、英文

    IF OBJECT_ID('DBO.GET_NUMBER2') IS NOT NULL DROP FUNCTION DBO.GET_NUMBER2 GO )) ) AS BEGIN BEGIN ,'' ...

  4. php pi()函数 语法

    php pi()函数 语法 pi()函数是什么意思? php pi()函数用于获取圆周率值,语法是pi(),这个函数只是单纯的用来获取圆周率值深圳大理石平台 作用:获取圆周率值 语法:pi() 参数: ...

  5. paper 156:专家主页汇总-计算机视觉-computer vision

    持续更新ing~ all *.files come from the author:http://www.cnblogs.com/findumars/p/5009003.html 1 牛人Homepa ...

  6. [CSP-S模拟测试]:reverse(模拟)

    题目传送门(内部题56) 输入格式 第一行包含一个整数:$T$,表示数据组数.接下来$T$行,每行包含两个字符串:$a\ b$. 输出格式 对于每组数据,如果存在$c$,输出最长的情况下字典序最大的$ ...

  7. 转载:IDEA配置SVN及使用

    转自:https://blog.csdn.net/zwj1030711290/article/details/80687365 1.安装svn客户端 之前用myEcplise只需要插件,现在IDEA需 ...

  8. MSSQL数据库表结构无法更改

    工具->Designers-> 组织保存要求重新创建表的更改 -> 把这个钩去掉就可以了    

  9. pycharm运行html文件报404错误

    某次发现运行导入的html文件的时候,打开浏览器会报404错误: 而运行自己写的html文件则正常显示: 最后发现这是pycharm缓存问题,只需重启pycharm清除缓存就ok啦!

  10. pip install 报SSL异常和timeout异常

    在安装pip3 install virtualenv时报了SSL异常 如图 pip is configured with locations that require TLS/SSL, however ...