当前位置:网站首页>力扣 989. 数组形式的整数加法
力扣 989. 数组形式的整数加法
2022-07-07 17:53:00 【Tomorrowave】
989. 数组形式的整数加法
整数的 数组形式 num 是按照从左到右的顺序表示其数字的数组。
例如,对于 num = 1321 ,数组形式是 [1,3,2,1] 。
给定 num ,整数的 数组形式 ,和整数 k ,返回 整数 num + k 的 数组形式 。
涉及知识点
字符串的转换
class Solution:
def addToArrayForm(self, A: List[int], K: int) -> List[int]:
K = list(map(int,str(K)))
res = []
i,j = len(A)-1,len(K)-1
carry = 0
while i >= 0 and j >= 0:
res.append(A[i] + K[j] + carry)
res[-1],carry = res[-1] % 10, res[-1] // 10
i -= 1
j -= 1
while i >= 0:
res.append(A[i] + carry)
res[-1],carry = res[-1] % 10, res[-1] // 10
i -= 1
while j >= 0:
res.append(K[j] + carry)
res[-1],carry = res[-1] % 10, res[-1] // 10
j -= 1
if carry:
res.append(1)
return res[::-1]
map()函数
也就是说,map可以对于本题的数组进行映射
del square(x):
return x ** 2
map(square,[1,2,3,4])#[1,4,9,16]
map(int,'1234') #[1,2,3,4]
class Solution:
def addToArrayForm(self, num: List[int], k: int) -> List[int]:
return [int(i) for i in str((int(str(num)[1:-1:3]))+k)]
or:
return list(map(int,str(int(''.join(str(num))) + K)))
边栏推荐
- R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图、设置palette参数自定义不同水平小提琴图的填充色、add参数在小提琴图添加箱图
- Matplotlib drawing 3D graphics
- 毕业季|遗憾而又幸运的毕业季
- 我的创作纪念日
- R语言使用ggplot2函数可视化需要构建泊松回归模型的计数目标变量的直方图分布并分析构建泊松回归模型的可行性
- Openeuler prize catching activities, to participate in?
- Sword finger offer II 013 Sum of two-dimensional submatrix
- Redis——基本使用(key、String、List、Set 、Zset 、Hash、Geo、Bitmap、Hyperloglog、事务 )
- IP tools
- How to cooperate among multiple threads
猜你喜欢
Simulate the implementation of string class
Make insurance more "safe"! Kirin Xin'an one cloud multi-core cloud desktop won the bid of China Life Insurance, helping the innovation and development of financial and insurance information technolog
PMP每日一练 | 考试不迷路-7.7
Kirin Xin'an won the bid for the new generation dispatching project of State Grid!
【STL】vector
mock.js从对象数组中任选数据返回一个数组
LeetCode_ 7_ five
模拟实现string类
Make this crmeb single merchant wechat mall system popular, so easy to use!
【RT-Thread env 工具安装】
随机推荐
mock. JS returns an array from the optional data in the object array
[RT thread env tool installation]
[sword finger offer] sword finger offer II 012 The sum of left and right subarrays is equal
使用高斯Redis实现二级索引
PMP每日一练 | 考试不迷路-7.7
841. 字符串哈希
el-upload上传组件的动态添加;el-upload动态上传文件;el-upload区分文件是哪个组件上传的。
一锅乱炖,npm、yarn cnpm常用命令合集
Ucloud is a basic cloud computing service provider
Semantic slam source code analysis
Is PMP beneficial to work? How to choose a reliable platform to make it easier to prepare for the exam!!!
R language ggplot2 visualization: use the ggqqplot function of ggpubr package to visualize the QQ graph (Quantitative quantitative plot)
openEuler 资源利用率提升之道 01:概论
[confluence] JVM memory adjustment
How to cooperate among multiple threads
Time tools
Matplotlib drawing 3D graphics
谷歌seo外链Backlinks研究工具推荐
浏览积分设置的目的
tp6 实现佣金排行榜