leetcode989
class Solution:
def addToArrayForm(self, A, K):
i = len(A) - 1
while i >= 0 and K > 0:
A[i] += K
K = A[i] // 10
if A[i] >= 10:
A[i] %= 10
if i == 0:
A = [0] + A
i += 1
i -= 1
return A
上面这个是参考别人的解决方案,思路不好理解,我又从新写了一个啰嗦的:
class Solution:
def addToArrayForm(self, A: 'List[int]', K: 'int') -> 'List[int]':
lenA = len(A)
KS = str(K)
lenB = len(KS)
B = list()
maxlen = 0
if lenA > lenB:
maxlen = lenA
dis = lenA - lenB
for i in range(dis):
B.append(0)
elif lenA < lenB:
maxlen = lenB
dis = lenB - lenA
for i in range(dis):
A.insert(0,0)
else:
maxlen = lenA for i in range(lenB):
B.append(int(KS[i])) I = 0
R = list()
maxpotion = maxlen - 1
while maxpotion >= 0:
C = A[maxpotion] + B[maxpotion] + I
if C >= 10:
I = 1
C = C % 10
else:
I = 0
R.insert(0,C)
maxpotion -= 1 if I == 1:
R.insert(0,1)
return R
leetcode989的更多相关文章
- [Swift]LeetCode989. 数组形式的整数加法 | 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. ...
随机推荐
- pytest.3.Assert
From: http://www.testclass.net/pytest/assert/ Assert就是断言,每个测试用例都需要断言. 与unittest不同,pytest使用的是python自带 ...
- 关于JAVA架构师
在我们行业内,我们大致把程序员分为四级 1.初级Java程序员的重心在编写代码.运用框架: 2.中级Java程序员重心在编写代码和框架: 3.高级Java程序员技术攻关.性能调优: 4.架构师 解决业 ...
- 【Graphite学习】系列学习文章-【转】
Graphite 系列 #2:Carbon 和 Whisper GRAPHITE SERIES #1: PROVISION HARDWARE GRAPHITE SERIES #2: CARBON &a ...
- SQL Server的分页优化及Row_Number()分页存在的问题
最近有项目反应,在服务器CPU使用较高的时候,我们的事件查询页面非常的慢,查询几条记录竟然要4分钟甚至更长,而且在翻第二页的时候也是要这么多的时间,这肯定是不能接受的,也是让现场用SQLServerP ...
- ueditor 正在读取目录及网络链接错误
环境 ueditor1_3_5-gbk-net .NET版本3.5 如果把项目直接改成4.0不会出现这样的问题,查看 问题1:正在读取目录 找到ueditor/ueditor.config.js 找 ...
- VLAN 及 GVRP 配置
一.VLAN配置 +进入vlan视图,如果指定的vlan没有创建则先创建它 [undo]vlan vlan_id undo vlan 剔除已创建的vlan VLAN_id:要进入的或要创建并进入的VL ...
- [UE4]C++中的注释
- [UE4]扔枪
1.把枪Detach掉:DetachFromActor 3个都选择“Keep World” 2.模拟物理 3.给一个向前的速度 4.切枪,到上一个武器,或者捡起脚底下的武器 注意Get Compone ...
- Sharing Configuration in ASP.NET Core SPA Scenarios
https://blogs.msdn.microsoft.com/webdev/2017/10/27/sharing-configuration-in-asp-net-core-spa-scenari ...
- psql: could not connect to server: No such file or directory&&PGHOST
由于环境变量 PGHOST配置不当引起的 postgres@pgdb-> psql psql: could not connect to server: No such file or dire ...