【LeetCode】989. Add to Array-Form of Integer 解题报告(C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/add-to-array-form-of-integer/
题目描述
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 <= A.length <= 10000
0 <= A[i] <= 9
0 <= K <= 10000
- If
A.length > 1
, thenA[0] != 0
题目大意
数组A表示了一个整数,K表示了一个整数,把两个数字相加,要求结果也是个数组形式的整数。
解题方法
数组转整数再转数组
一个比较偷懒的方法就是把数组转成整数然后相加,再转成数组的形式。代码也很简单。
python代码如下:
class Solution(object):
def addToArrayForm(self, A, K):
"""
:type A: List[int]
:type K: int
:rtype: List[int]
"""
ans = int("".join(map(str, A))) + K
if ans == 0:
return [0]
res = []
while ans:
res.append(ans % 10)
ans /= 10
return res[::-1]
模拟加法
这个题其实很类似链表加法2. Add Two Numbers,只不过是数组和整数的加法,做起来可以用类似的方式。
我下面的做法就是模拟加法,从低位开始,使用模拟进位的方式,求对应位数字的和。
注意两个数字相加的时候可能不是等长的,所以终止的条件有三个:数组数字用完了、K等于0了、进位也是0了。这三个条件同时满足的时候,才是真正的加法结束的时候。
在做这个题的过程中,如果每次使用res.insert(res.begin(), add)
的方法,每次插入的时间是O(N)的,导致最后的代码时间很长。如果使用下面的方式,每次插入到结果的后面,最后再翻转,那么时间会大幅缩短。
C++代码如下:
class Solution {
public:
vector<int> addToArrayForm(vector<int>& A, int K) {
const int M = A.size();
int carry = 0;
vector<int> res;
int i = M - 1;
while (i >= 0 || K != 0 || carry != 0) {
int a = (i < 0) ? 0 : A[i];
int add = a + K % 10 + carry;
if (add >= 10) {
carry = 1;
add -= 10;
} else
carry = 0;
res.push_back(add);
K /= 10;
--i;
}
reverse(res.begin(), res.end());
return res;
}
};
日期
2019 年 2 月 21 日 —— 一放假就再难抓紧了
【LeetCode】989. Add to Array-Form of Integer 解题报告(C++)的更多相关文章
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【LeetCode】1012. Complement of Base 10 Integer 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】922. Sort Array By Parity II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用奇偶数组 排序 奇偶数位置变量 日期 题目地址: ...
- 【LeetCode】915. Partition Array into Disjoint Intervals 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/partitio ...
- 【LeetCode】842. Split Array into Fibonacci Sequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】548. Split Array with Equal Sum 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 日期 题目地址:https://leetcode ...
- 【LeetCode】380. Insert Delete GetRandom O(1) 解题报告(Python)
[LeetCode]380. Insert Delete GetRandom O(1) 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxu ...
- 【LeetCode】873. Length of Longest Fibonacci Subsequence 解题报告(Python)
[LeetCode]873. Length of Longest Fibonacci Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
随机推荐
- windows下的python安装pysam报错
安装pysam时报错: 指定版本仍报错: 使用pysam-win安装: 但是import时不行: 貌似pysam在windows下难以正常配置,还是在Linux中用吧. https://www.jia ...
- Linux实现批量添加用户及随机密码小脚本
通过chpasswd命令可实现迅速为用户批量设置密码 实例:写一个脚本,实现批量添加20个用户user1-20,密码为用户名和后面跟5个随机字符 #!/bin/sh # 思路:通过for循环, ...
- void * 指针和const 指针
1.void * 是不能进行运算的,例如void *p p++; 这2个值是没有任何规律的. 2 .printf的时候打印void *p 指向的数据,必须强制类型转换,因为编译器不知道取地址多少位. ...
- 17.Power of Four-Leetcode
#define IMIN numeric_limits<int>::min() #define IMAX numeric_limits<int>::max() class So ...
- 【leetcode】451. Sort Characters By Frequency
Given a string s, sort it in decreasing order based on the frequency of the characters. The frequenc ...
- Java打jar包详解
Java打jar包详解 一.Java打jar包的几种方式https://www.cnblogs.com/mq0036/p/8566427.html 二.MANIFEST.MF文件详解https://w ...
- Java——数组的定义与使用
数组的定义与使用 1.数组的基本概念 (1)数组的动态初始化: 数组首先先开辟内存空间,而后再使用索引进行内容的设置,这种定义数组的方式称为动态初始化 数组是引用数据类型,存在有内存分配问题.在使用前 ...
- java加密方式
加密,是以某种特殊的算法改变原有的信息数据,使得未授权的用户即使获得了已加密的信息,但因不知解密的方法,仍然无法了解信息的内容.大体上分为双向加密和单向加密,而双向加密又分为对称加密和非对称加密(有些 ...
- When do we pass arguments by reference or pointer?
在C++中,基于以下如下我们通过以引用reference的形式传递变量. (1)To modify local variables of the caller function A reference ...
- CSS3新增特性\HTML标签类型
RGBA:透明度 作用: 设置透明度(R G B A) opacity:不透明度 文字也会被设置不透明度 圆角 border-radius:圆角{左上角,右上角.. ...