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 ...
随机推荐
- Android源码——AsynTask
AsyncTask<Params, Progress, Result>中三个参数为: Params 输入数据 Progress 过程数据 Result ...
- sql server 读取excel里的数据
以下是执行的sql代码,只拿简单读取数据举例,其他详细的,请自行查看 reconfigure RECONFIGURE GO GO SELECT * FROM OPENROWSET('Microsoft ...
- 利用scrapy和MongoDB来开发一个爬虫
今天我们利用scrapy框架来抓取Stack Overflow里面最新的问题(),并且将这些问题保存到MongoDb当中,直接提供给客户进行查询. 安装 在进行今天的任务之前我们需要安装二个框架,分别 ...
- note
John的博客 http://blog.sina.com.cn/chinatownjohn 剑4-11真题+新东方pdf,王陆语料库(听力)+顾家北手把手教你写剑9版(写作)+人人雅思哥记忆卡(口语) ...
- php验证身份证号码的正确性
/********************php验证身份证号码是否正确函数*********************/function is_idcard( $id ) { $id = strto ...
- React视角下的轮播图
天猫购物网站最显眼的就是轮播图了.我在学习一样新js库,一个新框架或新的编程思想的时候,总是感叹"入门必做选项卡,进阶须撸轮播图."作为一个React组件,它是状态操控行为的典型, ...
- JavaScript - 原型
一切皆为对象 殊不知,JavaScript的世界中的对象,追根溯源来自于一个 null 「一切皆为对象」,这句着实是一手好营销,易记,易上口,印象深刻. 万物初生时,一个null对象,凭空而生,接着O ...
- c++文件输入输出流fstream,对输入>>和输出<<重载
1. fstream 继承自iostream --> 要包含头文件#include<fstream> 2. 建立文件流对象 3. 打开文件夹 4. 测试是否打开成功 5. 进行读写操 ...
- MySQL 子查询与连接操作笔记
SQL语句之间是可以进行连接操作的,在一些复杂的数据操作中必须用到连接操作.简单的说就是一个SQL语句的结果可以作为相连接的SQL操作的一部分.SQL结构化查询语句,子查询是指的所有的SQL操作,并非 ...
- JS心得——判断一个对象是否为空
判断一个对象是否为空对象,本文给出三种判断方法: 最常见的思路,for...in...遍历属性,为真则为"非空数组":否则为"空数组" 2.通过JSON自带的. ...