题目如下:

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:

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. 1 <= A.length <= 10000
  2. 0 <= A[i] <= 9
  3. 0 <= K <= 10000
  4. If A.length > 1, then A[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的更多相关文章

  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. Arthas阿里开源的 Java 诊断工具

    当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决: 1.这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception? 2.我改的代码为什么没有执行到?难道是我没 commi ...

  2. delphi 半透明窗体类

    {******************************************************************************* 半透明窗体控件 版本:1.0 功能说明 ...

  3. shell(计算机壳层)(二)

    shell 命令常用命令cat 文件名 输出文件内容到基本输出(屏幕 or 加>fileName 到另一个文件)cb 格式化源代码chmod //change mode,改变文件的权限cp co ...

  4. 做Data Mining,其实大部分时间都花在清洗数据

    做Data Mining,其实大部分时间都花在清洗数据 时间 2016-12-12 18:45:50  51CTO 原文  http://bigdata.51cto.com/art/201612/52 ...

  5. mongodbdriver 的C# 驱动findasync变成列表的方法

    IAsyncCursorExtensions.ToList(返回的Task<IAsyncCursor<T>>实例). 也有他的异步版本.可以参见 https://mongodb ...

  6. CentOS 7在VMware 12中共享文件看不见的问题?

    前言 由于rhel 7.2因为没有注册导致yum无法使用,包括自己配置本地源,这个命令在你没有注册都不能使用,每次使用rpm去装软件,自己去找缺少的依赖包,实在是麻烦.于是不如就换一个系统,CentO ...

  7. java并发编程笔记(十一)——高并发处理思路和手段

    java并发编程笔记(十一)--高并发处理思路和手段 扩容 垂直扩容(纵向扩展):提高系统部件能力 水平扩容(横向扩容):增加更多系统成员来实现 缓存 缓存特征 命中率:命中数/(命中数+没有命中数) ...

  8. SharePoint 2013中PerformancePoint仪表板设计器连接Analysis Services 2012的问题

    在SharePoint 2013的PerformancePoint仪表板设计器在创建链接到AnalysisServices 2012的数据链接的时候,数据库列表无法获取服务器上的数据库.这个问题挺让人 ...

  9. 20150721—HTML的定位 JS (转)

    本文转载于:http://blog.csdn.net/xuantian868/article/details/3116442   HTML:scrollLeft,scrollWidth,clientW ...

  10. 测开之路三十六:常用的css选择器

    在static下新建一个css,并写入内容 /*标签选择器,label标签的颜色为红色*/label {color: red;} /*.代表类选择器,绿色*/.test {color: green;} ...