2. Add Two Numbers——Python
题目:
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
代码:
该题目其实就是链表中的数字取出来,然后相加,不过注意高位数字在后面,需要倒过来。比如题目例子中就是要:342+465=807,之后把807每一位从小到大记录在一个链表里。
于是,我用了最常规的办法,不过也是解决了问题的:
#coding:utf-8
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
if not l1 and not l2:return
#取出链表中的数字存入数组
arr1,arr2=[],[]
while l1:
arr1.append(l1.val)
l1 = l1.next
while l2:
arr2.append(l2.val)
l2 = l2.next
#倒序
arr1.reverse()
arr2.reverse()
#print (arr1,arr2)
#组成数字
num1,num2 = 0,0
for i in arr1:
num1 = num1*10+i
for i in arr2:
num2 = num2*10+i
print (num1,num2)
#相加
num_total = num1+num2
print (num_total)
#从低位到高位写入链表,初始化链表的根节点为0,如果相加的和为0,直接返回
l_res = ListNode(0)
cursor = l_res
if num_total == 0: return l_res
while num_total:
temp = num_total%10
print (temp)
cursor.next = ListNode(temp)
cursor = cursor.next
num_total = int(num_total/10)
#print (num_total)
return l_res.next if __name__=='__main__':
#创建l1和l2两个链表,注意,排序好的就需要arr1和arr2中数字从小到大
arr1 = [0,8,6,5,6,8,3,5,7]
arr2 = [6,7,8,0,8,5,8,9,7]
l1 = ListNode(arr1[0])
p1 = l1
l2 = ListNode(arr2[0])
p2 = l2
for i in arr1[1:]:
p1.next = ListNode(i)
p1 = p1.next
for i in arr2[1:]:
p2.next = ListNode(i)
p2 = p2.next
s=Solution()
#两个链表相加
q=s.addTwoNumbers(l1,l2)
一些打印的输出:
753865680 798580876
1552446556
6
5
5
6
4
4
2
5
5
1
2. Add Two Numbers——Python的更多相关文章
- Leetcode2:Add Two Numbers@Python
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- Leetcode 解题 Add Two Numbers Python
原题: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- leetcode add two numbers python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...
- (python)leetcode刷题笔记 02 Add Two Numbers
2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...
- LeetCode 2. add two numbers && 单链表
add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
随机推荐
- 【hihocoder#1413】Rikka with String 后缀自动机 + 差分
搞了一上午+接近一下午这个题,然后被屠了个稀烂,默默仰慕一晚上学会SAM的以及半天4道SAM的hxy大爷. 题目链接:http://hihocoder.com/problemset/problem/1 ...
- 项目中angular js的接口url统一管理
为了防止环境改变时需要修改多处接口的url,项目中用到了一个config.json文件来统一管理url: 在src下建立config文件夹,创建config.json文件,主要内容如下: { &quo ...
- 【原创】自己动手写工具----签到器[Beta 2.0]
一.前面的话 上一篇中基本实现了简单的签到任务,但是不够灵活.在上一篇自己动手写工具----签到器的结尾中,我设想了几个新增功能来提高工具的灵活程度,下面把新增功能点列出来看看: (1)新增其他的进程 ...
- WinForm------DateEdit属性设置
1.只能选择年份属性设置
- Python Day12
MySQL 数据库介绍 什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库, 每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据. 我们 ...
- Swift中的可选链与内存管理(干货系列)
干货之前:补充一下可选链(optional chain) class A { var p: B? } class B { var p: C? } class C { func cm() -> S ...
- Java多线程 5 多线程其他知识简要介绍
一.线程组 /** * A thread group represents a set of threads. In addition, a thread * group can also inclu ...
- svn强制加注释才能提交
进入库的hooks目录下 cp pre-commit.tmpl pre-commit 并对pre-commit加入运行权限 修改pre-commit内容如下 REPOS="$1" ...
- Download Excel file with Angular
源码连接(编写中) 用Angular下载后台返回的Excel文件,用Blob实现,引用FileSaver.js 后台C#代码: [WebMethod] public static byte[] Cal ...
- hub,桥,交换机,路由器的区别
1.四种设备在网络中的物理位置 如下图 2.这四种设备的本质 这四种设备,不管怎样,他们都是进行包的转发,只不过转发的行为有些不一样而已 3.逐一介绍 对于hub,一个包过来后,直接将包转发到其他口. ...