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 ...
随机推荐
- OO第一次总结作业
第一次OO博客作业 前言 面向对象课程已经经过了4周的时间.前三次作业全部是关于多项式求导的相关内容,内容由易到难,同时我也开始逐渐深入感受学习面向对象的各项特征,逐渐将自己的编程风格从C向真正的面向 ...
- #leetcode刷题之路36-有效的数独
判断一个 9x9 的数独是否有效.只需要根据以下规则,验证已经填入的数字是否有效即可.数字 1-9 在每一行只能出现一次.数字 1-9 在每一列只能出现一次.数字 1-9 在每一个以粗实线分隔的 3x ...
- Keil C51编译报错error C141: syntax error
错误代码: typedef unsigned char uchar uchar KeyRowColumnScan() { GPIO_KEY = 0x0f; uchar key_value = ; // ...
- 基于bootstrap的文本编辑器组件:Summernote
Summernote官网地址 :https://summernote.org/ 这是官网的一个例子: <!DOCTYPE html> <html lang="en" ...
- 18 [网络编程]-UDP
1.TCP VS UDP tcp基于链接通信 基于链接,则需要listen(backlog),指定连接池的大小 基于链接,必须先运行的服务端,然后客户端发起链接请求 对于mac系统:如果一端断开了链接 ...
- 洛咕 P3965 [TJOI2013]循环格
同tjoi2010 打扫房间,每个点入度,出度都为1,可以向相邻4个点连边,但只有原来存在的边费用为0. // luogu-judger-enable-o2 #include<bits/stdc ...
- 洛咕 P2467 [SDOI2010]地精部落
同波浪,简单dp. 高度从1到n插入山脉,设f[i][j][k]表示插入了i个山脉,组成了j段,边界上有k个山脉的方案数. 那么新插入的山脉只会:插入在边界上且自己是一段.插入在边界上且与最左边的段相 ...
- [坑况]——webpack搭建前端环境踩过的坑啊
前言 嘿哈,webpack搭建前端环境踩过的坑啊! 第一个:完全不知所措 webpack4 下面用不了HtmlWebpackPlugin 和 ExtractTextPlugin 解决方案: html- ...
- 服务器路由配置--Route
第1章 命令配置 虚拟服务器 网卡配置信息 虚拟网卡名称 虚拟网卡模式 服务器01 eth1 10.0.0.10/24 nat模式 服务器02 eth2 10.0.0.11/24 nat模式 eth3 ...
- 配置centos7来支持xshell远程访问和xftp传输文件
前提: 首先需要一台已装有centos7的电脑(虚拟机的配置这里不说明,这里用的是物理机) 背景: 在工作中访问linux的环境通常需要Xshell等终端软件,通过配置静态IP远程服务器进行管理开发. ...