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. ...
随机推荐
- Python中的self和init
From: https://www.crifan.com/summary_the_meaning_of_self_and___init___in_python_and_why_need_them/ 背 ...
- pytest.1.快速开始
From: http://www.testclass.net/pytest/quick_start/ 简介 pytest测试框架可以让我们很方便的编写测试用例,这些用例写起来虽然简单,但仍然可以规模化 ...
- C++进阶--隐式类型转换
//############################################################################ /* 隐式类型转换 * * 类型转换可分为 ...
- 【转】hive中UDF、UDAF和UDTF使用
原博文出自于: http://blog.csdn.net/liuj2511981/article/details/8523084 感谢! Hive进行UDF开发十分简单,此处所说UDF为Tempora ...
- Redis的5中数据类型
Radis的作用相信既然然就就知道她的作用,但是对于刚开始对radis学习的初学者来说,理解起来比较费劲.这里就从开始一步步认识radis 首先要知道radis是存在内存中的数据,所以读取速度回更改, ...
- vc++使用IWinHttpRequest获取网页内容乱码
mfc项目的字符集为unicode字符集 乱码前代码: CString rspStr = pHttpReq->ResponseText; MessageBox(rspStr); 乱码效果: 解决 ...
- Survival Analysis
code{white-space: pre;} Survival Analysis Zhu Lin 2017-3-18 What is Survival Analysis Survival analy ...
- 字符串分割split()
知识讲解: split() 方法将字符串分割为字符串数组,并返回此数组. 语法: stringObject.split(separator,limit) 参数说明: 注意:如果把空字符串 (" ...
- 跨域的案例 以百度接口/手写接口为例,还有jQuery写法
仅在js部分输入即可 百度接口的案例 <script> function fn(data){ console.log(data) } </script> <script ...
- 安装httpd服务配置
本地yum源安装 mkdir /opt/dvd (先用mkdir去根下opt目录下建一个名字叫dvd的目录) mount /dev/sr0 /opt/dvd (用mount命令,挂载光盘设备 ...