Leetcode题库——1.两数之和
@author: ZZQ
@software: PyCharm
@file: addTwoNumbers.py
@time: 2018/9/18 10:35
要求:给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,它们的每个节点只存储单个数字。将两数相加返回一个新的链表。
你可以假设除了数字 0 之外,这两个数字都不会以零开头。
e.g.: 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Solution():
def __init__(self):
pass
def addsubtwonumber(self, *args):
arg_len =len(*args)
sum = 0
for arg in range(arg_len):
sum += args[0][arg]
return sum % 10, sum / 10
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
if l1 is None and l2 is None:
return None
if l1 is None:
return l2
if l2 is None:
return l1
l3 = ListNode(0)
head = l3
temp = 0
temp_rem = ListNode(temp)
while l1 is not None and l2 is not None:
s, rem = self.addsubtwonumber([l1.val, l2.val, temp_rem.val])
l3.next = ListNode(s)
temp_rem.val = rem
l3 = l3.next
l2 = l2.next
l1 = l1.next
temp = 0
# temp_rem.val = temp
if l1 is None and l2 is not None:
while l2 is not None:
s, rem = self.addsubtwonumber([l2.val, temp_rem.val])
l3.next = ListNode(s)
l3 = l3.next
l2 = l2.next
temp_rem.val = rem
else:
if l1 is not None and l2 is None:
while l1 is not None:
s, rem = self.addsubtwonumber([l1.val, temp_rem.val])
l3.next = ListNode(s)
l3 = l3.next
l1 = l1.next
temp_rem.val = rem
if temp_rem.val != 0:
l3.next = ListNode(temp_rem.val)
return head.next
if __name__ == "__main__":
answer = Solution()
l1 = ListNode(9)
l1.next = ListNode(8)
l2 = ListNode(1)
l3 = answer.addTwoNumbers(l1, l2)
while l3 is not None:
print l3.val
l3 = l3.next
Leetcode题库——1.两数之和的更多相关文章
- Leetcode题库——15.三数之和
@author: ZZQ @software: PyCharm @file: threeSum.py @time: 2018/10/6 19:47 说明:给定一个包含 n 个整数的数组 nums,判断 ...
- Leetcode:0002(两数之和)
LeetCode:0002(两数之和) 题目描述:给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表.你可以假设除了数字 0 之外,这两 ...
- Newtonsoft.Json C# Json序列化和反序列化工具的使用、类型方法大全 C# 算法题系列(二) 各位相加、整数反转、回文数、罗马数字转整数 C# 算法题系列(一) 两数之和、无重复字符的最长子串 DateTime Tips c#发送邮件,可发送多个附件 MVC图片上传详解
Newtonsoft.Json C# Json序列化和反序列化工具的使用.类型方法大全 Newtonsoft.Json Newtonsoft.Json 是.Net平台操作Json的工具,他的介绍就 ...
- Leetcode(1)两数之和
Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...
- [LeetCode] 1. Two Sum 两数之和
Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...
- LeetCode Golang实现 1. 两数之和
1. 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这 ...
- Leetcode(一)两数之和
1.两数之和 题目要求: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重 ...
- LeetCode题解001:两数之和
两数之和 题目 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个 ...
- [Leetcode] Add two numbers 两数之和
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
随机推荐
- C++STL之unordered_map与QT的QHash对比
刚刚心血来潮,试一试QT和STL哪个好 网上评论都支持STL,我试了试: 贴上代码: #include <QCoreApplication> #include <QHash> ...
- 用HTML编写淘宝页面
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...
- Delphi - SPcomm 控件使用
Com口函数 自动获取Com口,函数列表 procedure EnumComPorts(Ports: TStrings); //自动获取com口函数 var KeyHandle: HKEY; ErrC ...
- 【安装Ecshop2.7.2网站(LAMP环境)】--实践
LAMP : Linux + Apache + Mysql + PHPEcshop2.7.2 注意:在输入命令过程中,学会用tab键补全命令,不要对着照抄,很容易出错. 前置:A:先设置虚拟机中的CD ...
- 如何保障Go语言基础代码质量?
为什么要谈这个topic? 实践中,质量保障体系的建设,主要针对两个目标: 一是不断提高目标业务测试覆盖率,保障面向客户的产品质量:二就是尽可能的提高人效,增强迭代效率.而构建全链路质量卡点就是整个体 ...
- springboot快速入门(二)——项目属性配置(日志详解)
一.概述 application.properties就是springboot的属性配置文件 在使用spring boot过程中,可以发现项目中只需要极少的配置就能完成相应的功能,这归功于spring ...
- 4538: [Hnoi2016]网络
4538: [Hnoi2016]网络 链接 分析: 整体二分. 对于一次操作,可以二分一个答案mid,判断权值大于mid的路径是否全部经过这个点.如果是 ,那么这次询问的答案在[l,mid-1]之间, ...
- python易错盲点排查之+=与+的区别分析以及一些赋值运算踩过的坑
问题1. int和list是不一样的 >>> a=1 >>> b=a >>> a+=1 >>> a,b (2, 1) >& ...
- javaweb学习4——HttpServletRequest的使用
声明:本文只是自学过程中,记录自己不会的知识点的摘要,如果想详细学习JavaWeb,请到孤傲苍狼博客学习,JavaWeb学习点此跳转 本文链接:https://www.cnblogs.com/xdp- ...
- php_package v2.7发布了 宋正河作品
php_package 是一个面向过程的底层开发框架 http://download.csdn.net/download/songzhengdong82/4974123 欢迎大家下载