当前位置:网站首页>力扣 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)))
边栏推荐
- Version selection of boot and cloud
- LC:字符串转换整数 (atoi) + 外观数列 + 最长公共前缀
- Kubernetes——kubectl命令行工具用法详解
- LeetCode_7_5
- Le PGR est - il utile au travail? Comment choisir une plate - forme fiable pour économiser le cœur et la main - d'œuvre lors de la préparation de l'examen!!!
- 8 CAS
- ASP.NET幼儿园连锁管理系统源码
- 模拟实现string类
- Notes...
- how to prove compiler‘s correctness
猜你喜欢
【RT-Thread env 工具安装】
Simulate the implementation of string class
openEuler 资源利用率提升之道 01:概论
Ways to improve the utilization of openeuler resources 01: Introduction
CSDN syntax description
LeetCode_7_5
剑指 Offer II 013. 二维子矩阵的和
Kunpeng developer summit 2022 | Kirin Xin'an and Kunpeng jointly build a new ecosystem of computing industry
Flink并行度和Slot详解
ASP.NET幼儿园连锁管理系统源码
随机推荐
力扣 643. 子数组最大平均数 I
Dynamic addition of El upload upload component; El upload dynamically uploads files; El upload distinguishes which component uploads the file.
vulnhub之school 1
ASP. Net gymnasium integrated member management system source code, free sharing
My creation anniversary
Throughput
LeetCode力扣(剑指offer 36-39)36. 二叉搜索树与双向链表37. 序列化二叉树38. 字符串的排列39. 数组中出现次数超过一半的数字
Leetcode force buckle (Sword finger offer 36-39) 36 Binary search tree and bidirectional linked list 37 Serialize binary tree 38 Arrangement of strings 39 Numbers that appear more than half of the tim
关于cv2.dnn.readNetFromONNX(path)就报ERROR during processing node with 3 inputs and 1 outputs的解决过程【独家发布】
9 atomic operation class 18 Rohan enhancement
Kirin Xin'an cloud platform is newly upgraded!
Make this crmeb single merchant wechat mall system popular, so easy to use!
Semantic SLAM源码解析
R language dplyr package mutate_ At function and min_ The rank function calculates the sorting sequence number value and ranking value of the specified data column in the dataframe, and assigns the ra
毕业季|遗憾而又幸运的毕业季
841. String hash
841. 字符串哈希
项目经理『面试八问』,看了等于会了
PMP每日一练 | 考试不迷路-7.7
实训九 网络服务的基本配置