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. ...
随机推荐
- nginx简单学习(tomcat)
一.负载均衡的简单配置 1.下载nginx 2.tomcat*2 配置不同的端口用于正常启动,在jsp中<%= session.getId()%>可以查看jSessionId,tomcat ...
- 【Mysql】事务日志-Write Ahead logging vs command-logging(转)
原理讲解: Write Ahead logging vs command logging Write Ahead logging 持久化数据保存在磁盘,数据的存储是随机的,并非顺序: 内存中保存磁盘数 ...
- HDOJ 2005 第几天?
#include<iostream> using namespace std; ] = { ,,,,,,,,,,, }; ] = { ,,,,,,,,,,, }; bool isRYear ...
- CDlinux 安装
镜像 CDlinux-0.9.7.1 虚拟机VMware12 1.VMware12中,新建虚拟机 2.典型安装方式 下一步 3.稍后安装操作系统 4.内核版本要选择[其他linux2.6.X内核] 5 ...
- 一个源文件可以写出多个class吗?编译后,会不会生成多个class文件?
会.一个.java源文件里面可以有内部类.其他类(有且仅有一个类可以声明为public),所以编译后,可以有多个class文件.
- [UE4]蓝图函数库
在任何蓝图上都可以调用“蓝图函数库”的方法
- golang 常量的用法
1.Golang常量的用法 //常量的用法 var num int num =9 //1.常量声明的时候必须赋值 const tax int = 0 //2.常量值是无法修改的 //tax = 10 ...
- docker设置容器固定ip
docker安装后,默认会创建三种网络类型,bridge.host和none,可通过如下命令查看 sudo docker network ls 1 bridge:网络桥接 默认情况下启动.创建容器都是 ...
- POJ3635 Full Tank?
[题解] 用dijkstra算法求最短路.同时考虑在每个节点加油(一单位)与否. [代码] #include <iostream> #include <map> #includ ...
- javascript-回归原生基础
//添加事件监听兼容函数 function addHandler(target, eventType, handler){ if(target.addEventListener){//主流浏览器 ad ...