当前位置:网站首页>力扣题(2)—— 两数相加
力扣题(2)—— 两数相加
2022-07-30 22:06:00 【世界的隐喻】
两数相加
题目内容
给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。
请你将两个数相加,并以相同形式返回一个表示和的链表。
你可以假设除了数字 0 之外,这两个数都不会以 0 开头。
示例
示例 1:
输入:l1 = [2,4,3], l2 = [5,6,4]
输出:[7,0,8]
解释:342 + 465 = 807.
示例 2:
输入:l1 = [0], l2 = [0]
输出:[0]
示例 3:
输入:l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
输出:[8,9,9,9,0,0,0,1]
提示:
每个链表中的节点数在范围 [1, 100] 内
0 <= Node.val <= 9
题目数据保证列表表示的数字不含前导零
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/add-two-numbers
题解一:暴力解法
根据示例的解释,最先想到的方法就是将链表转为数字,然后数字相加再翻转,最后生成新链表。
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
# 循环输出 l1 链表的数字添加到列表中
l1_list = []
while True:
if l1 is None:
break
l1_list.append(l1.val)
l1 = l1.next
# 循环输出 l2 链表的数字添加到列表中
l2_list = []
while True:
if l2 is None:
break
l2_list.append(l2.val)
l2 = l2.next
# 将 l1 列表中的数字连接形成字符串
a = ""
for i in l1_list:
a += str(i)
# 将 l2 列表中的数字连接形成字符串
b = ""
for i in l2_list:
b += str(i)
# 将 l1 ,l2 形成的字符串翻转之后形成相加之后的数字
c = int(a[::-1]) + int(b[::-1])
c = str(c) # 数字转换为字符串
# 翻转最后的字符串,循环形成新的链表
for i in range(0, len(c)):
if i == 0:
res = ListNode(val = int(c[i]), next = None)
else:
res = ListNode(val = int(c[i]), next = res)
return res
力扣通过的时间大约是104 ms
题解二
根据示例一可以发现其实这是一个从最高位向最低位运算的竖式运算,并且根据示例三可以发现题目的竖式计算是最高位对齐的竖式计算。
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
# 循环输出 l1 链表的数字添加到列表中
l1_list = []
while True:
if l1 is None:
break
l1_list.append(l1.val)
l1 = l1.next
# 循环输出 l2 链表的数字添加到列表中
l2_list = []
while True:
if l2 is None:
break
l2_list.append(l2.val)
l2 = l2.next
res = []
l1_length = len(l1_list)
l2_length = len(l2_list)
num_ten = 0
# 考虑 l1 和 l2 链表不一样长的情况
min_length = min(l1_length, l2_length)
max_length = max(l1_length, l2_length)
for i in range(0, min_length): # 尾(头部)对齐
num = l1_list[i] + l2_list[i] + num_ten
num_ten = num // 10 # 10位数上的数字,即需要进位的数字
num_one = num % 10 # 个位数上的数字
res.append(num_one) # 根据竖式运算法则,保留个位数字,十位数进位
for i in range(min_length, max_length): # 长出的部分进行进位
if l1_length > min_length:
num = l1_list[i] + num_ten
num_ten = num // 10 # 10位数上的数字
num_one = num % 10 # 个位数上的数字
res.append(num_one)
elif l2_length > min_length:
num = l2_list[i] + num_ten
num_ten = num // 10 # 10位数上的数字
num_one = num % 10 # 个位数上的数字
res.append(num_one)
if num_ten != 0:
res.append(num_ten)
# 翻转并形成链表
res = res[::-1]
for i in range(0, len(res)):
if i == 0:
ans = ListNode(val = int(res[i]), next = None)
else:
ans = ListNode(val = int(res[i]), next = ans)
return ans
力扣通过的时间大约是56 ms
边栏推荐
- 【微信小程序】小程序突破小程序二维码数量限制
- 微信公众号授权登录后报redirect_uri参数错误的问题
- socket: Kernel initialization and detailed process of creating streams (files)
- 【Nacos】解决Nacos下载速度缓慢的问题
- ClickHouse 创建数据库建表视图字典 SQL
- MySQL删除表数据 MySQL清空表命令 3种方法
- 系统结构考点之CRAY-1向量处理机
- 登堂入室之soc开发makefile
- 3 minutes to take you to understand WeChat applet development
- navicat新建数据库
猜你喜欢

解决centos8 MySQL密码问题ERROR 1820 (HY000) You must reset your password using ALTER USER

CISP-PTE Zhenti Demonstration

Navicat connection MySQL error: 1045 - Access denied for user 'root'@'localhost' (using password YES)

MySQL compressed package installation, fool teaching

MySQL 8.0.29 解压版安装教程(亲测有效)

Detailed explanation of the delete problem of ClickHouse delete data

cnpm安装步骤

MySQL 8.0.29 set and modify the default password

A simple rich text editor

活动推荐 | 2022年深圳最值得参加的边缘计算活动
随机推荐
【科研】文献下载神器方式汇总
CISP-PTE真题演示
Union, the difference between union and structure, the knowledge of enumeration of C language corners
When Navicat connects to MySQL, it pops up: 1045: Access denied for user 'root'@'localhost'
MySQL 有这一篇就够(呕心狂敲37k字,只为博君一点赞!!!)
活动推荐 | 2022年深圳最值得参加的边缘计算活动
系统结构考点之流水线向量点积
The mysql time field is set to the current time by default
MySQL 灵魂 16 问,你能撑到第几问?
A simple rich text editor
【无标题】
Chrome 配置samesite=none方式
折叠旧版应用程序
ML.NET相关资源整理
Google Earth Engine ——快速实现MODIS影像NDVI动画的在线加载并导出
DistSQL 深度解析:打造动态化的分布式数据库
ELF: Loading process
系统结构考点之多级混洗交换网络
JDBC (detailed explanation)
【微信小程序】小程序突破小程序二维码数量限制