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 ...
随机推荐
- ubuntu16.04系统gcc下降和升级
gcc下降 1 安装 sudo apt-get install -y gcc-4.7 sudo apt-get install -y g++-4.7 2 重新建立软连接 cd /usr/bin #进入 ...
- vue 目录结构解析
├── README.md 项目介绍├── index.html 入口页面├── build 构建脚本目录│ ├── webpack.base.conf.js webpack基础配置,开发环境,生产环 ...
- ThinkPHP5.1中数据查询使用field方法数组参数起别名时遇到的问题
首先数据库基本查询是没有问题的 <?php namespace app\index\controller; use think\Db; class Demo5 { //1.单条查询 public ...
- day 92 跨域和CORS
跨域和CORS 本节目录 一 跨域 二 CORS 三 xxx 四 xxx 五 xxx 六 xxx 七 xxx 八 xxx 一 跨域 同源策略(Same origin policy)是一种约定, ...
- windows 8 中 使用 httpclient
基本技术点 windows 8 中 使用 httpclient 代替 windows phone 中的 httpwebclient , 使用方法 也有些不同 . 下面是windows 8种使用 htt ...
- PostgreSQL调整内存与IO的参数说明
磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面:PostgreSQL内部结构与源代码研究索引页 回到顶级页面:PostgreSQL索引页 [作者:高健@博客园 luckyjackgao ...
- c++ goto语句
#include <stdio.h> #include <math.h> int main(void) //main是程序入口 { int num; printf(" ...
- Django中表单的用法深探
[转载说明:原文排版不是很好,为方便阅读,改进了排版] django的表单设计真的很棒,涉及非常多的功能,今天介绍django较为主流的几种表单使用方法.注:本文中表单与form通用.模型与model ...
- 4557: [JLoi2016]侦察守卫
4557: [JLoi2016]侦察守卫 链接 分析: 因为D比较小,所设状态f[i][j]表示子树i内,从i往下第j层及第j层以下都覆盖了的最小代价,g[i][j]表示覆盖完子树内所有点,还可以往上 ...
- 洛谷 P1337 [JSOI2004]平衡点 / 吊打XXX
洛谷 P1337 [JSOI2004]平衡点 / 吊打XXX 点击进入FakeHu的模拟退火博客 神仙模拟退火...去看fakehu的博客吧...懒得写了... 因为精度问题要在求得的最优解附近(大约 ...