【leetcode】989. Add to Array-Form of Integer
题目如下:
For a non-negative integer
X
, the array-form ofX
is an array of its digits in left to right order. For example, ifX = 1231
, then the array form is[1,2,3,1]
.Given the array-form
A
of a non-negative integerX
, return the array-form of the integerX+K
.Example 1:
- Input: A = [1,2,0,0], K = 34
- Output: [1,2,3,4]
- Explanation: 1200 + 34 = 1234
Example 2:
- Input: A = [2,7,4], K = 181
- Output: [4,5,5]
- Explanation: 274 + 181 = 455
Example 3:
- Input: A = [2,1,5], K = 806
- Output: [1,0,2,1]
- Explanation: 215 + 806 = 1021
Example 4:
- Input: A = [9,9,9,9,9,9,9,9,9,9], K = 1
- Output: [1,0,0,0,0,0,0,0,0,0,0]
- Explanation: 9999999999 + 1 = 10000000000
Note:
1 <= A.length <= 10000
0 <= A[i] <= 9
0 <= K <= 10000
- If
A.length > 1
, thenA[0] != 0
解题思路:题目很简单,但是在相加的过程中要注意进位。同时K > A的场景需要做特殊处理。
代码如下:
- class Solution(object):
- def addToArrayForm(self, A, K):
- """
- :type A: List[int]
- :type K: int
- :rtype: List[int]
- """
- carrier = 0
- for i in range(len(A)-1,-1,-1):
- remainder = K % 10
- K = K/10
- A[i] += (remainder + carrier)
- if A[i] >= 10:
- A[i] -= 10
- carrier = 1
- else:
- carrier = 0
- if carrier == 1 and K == 0:
- A.insert(0,1)
- elif K > 0:
- K += carrier
- while K > 0:
- A.insert(0, K% 10)
- carrier = 0
- K = K/10
- return A
【leetcode】989. Add to Array-Form of Integer的更多相关文章
- 【LeetCode】989. Add to Array-Form of Integer 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组转整数再转数组 模拟加法 日期 题目地址:htt ...
- 【LeetCode】623. Add One Row to Tree 解题报告(Python)
[LeetCode]623. Add One Row to Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problem ...
- 【LeetCode】数组-6(561)-Array Partition I(比较抽象的题目)
题目描述:两句话发人深思啊.... Given an array of 2n integers, your task is to group these integers into n pairs o ...
- 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...
- 【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; 完
- 【LeetCode】258. Add Digits (2 solutions)
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- 【LeetCode】 258. Add Digits 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:减1模9 方法三:直接模9 日 ...
- 【LeetCode】912. Sort an Array 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数排序 桶排序 红黑树排序 归并排序 快速排序 ...
- 【LeetCode】415. Add Strings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...
随机推荐
- KEGG注释
在 KEGG 数据库中,把功能相似的蛋白质归为同一组,然后标上 KO 号.通过相似性比对,可以为未知功能的蛋白序列注释上 KO 号. 截止到 2015 年 6 月 12 日,KEGG 数据库中共收录了 ...
- Linux网络栈
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11394930.html OSI模型 OSI 模型把网络互联的框架分为应用层.表示层.会话层.传输层.网 ...
- sql判断中文、数字、英文
IF OBJECT_ID('DBO.GET_NUMBER2') IS NOT NULL DROP FUNCTION DBO.GET_NUMBER2 GO )) ) AS BEGIN BEGIN ,'' ...
- php pi()函数 语法
php pi()函数 语法 pi()函数是什么意思? php pi()函数用于获取圆周率值,语法是pi(),这个函数只是单纯的用来获取圆周率值深圳大理石平台 作用:获取圆周率值 语法:pi() 参数: ...
- paper 156:专家主页汇总-计算机视觉-computer vision
持续更新ing~ all *.files come from the author:http://www.cnblogs.com/findumars/p/5009003.html 1 牛人Homepa ...
- [CSP-S模拟测试]:reverse(模拟)
题目传送门(内部题56) 输入格式 第一行包含一个整数:$T$,表示数据组数.接下来$T$行,每行包含两个字符串:$a\ b$. 输出格式 对于每组数据,如果存在$c$,输出最长的情况下字典序最大的$ ...
- 转载:IDEA配置SVN及使用
转自:https://blog.csdn.net/zwj1030711290/article/details/80687365 1.安装svn客户端 之前用myEcplise只需要插件,现在IDEA需 ...
- MSSQL数据库表结构无法更改
工具->Designers-> 组织保存要求重新创建表的更改 -> 把这个钩去掉就可以了
- pycharm运行html文件报404错误
某次发现运行导入的html文件的时候,打开浏览器会报404错误: 而运行自己写的html文件则正常显示: 最后发现这是pycharm缓存问题,只需重启pycharm清除缓存就ok啦!
- pip install 报SSL异常和timeout异常
在安装pip3 install virtualenv时报了SSL异常 如图 pip is configured with locations that require TLS/SSL, however ...