当前位置:网站首页>02.两数相加
02.两数相加
2022-07-25 17:07:00 【用户5573316】
#02.两数相加
难度:中等
给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
示例:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
# 暴力法
# 思路
循环破解
首先定义一个根节点 0
然后定义一个变量cur 可以在循环中修改内存指向
然后定义一个temp 用来存进位数据,初始化为 0,若无进位重置为0
循环判断非空为val,空的话为0
最终返回根节点的子节点
# 代码
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int temp = 0;
ListNode pre = new ListNode(0);
ListNode listNode = pre;
while (l1 != null || l2 != null) {
int i = l1 == null ? 0 : l1.val;
int j = l2 == null ? 0 : l2.val;
int sum = i + j + temp;
temp = sum / 10;
sum = sum % 10;
listNode.next = new ListNode(sum);
listNode = listNode.next;
if (l1 != null) {
l1 = l1.next;
}
if (l2 != null) {
l2 = l2.next;
}
}
if(temp == 1) {
listNode.next = new ListNode(temp);
}
return pre.next;
}
# 递归法
# 思路
在递归方法中先判断l1是否为空且 l2 是否为空且进位值是否为0
然后在调用递归时判断 l1 是否为空,l2 是否为空
# 代码
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
return fun(l1, l2, 0);
}
ListNode fun(ListNode l1, ListNode l2, int temp) {
if (l1 == null && l2 == null && temp == 0) {
return null;
}
int i = l1 == null ? 0 : l1.val;
int j = l2 == null ? 0 : l2.val;
int sum = i + j + temp;
temp = sum / 10;
sum = sum % 10;
ListNode listNode = new ListNode(sum);
listNode.next = fun(l1 != null ? l1.next : null, l2 != null ? l2.next : null, temp);
return listNode;
}
}
边栏推荐
- QT listview list display component notes
- 【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part3):基于规则的问题分类
- 搜狗批量推送软件-搜狗批量推送工具【2022最新】
- 7.依赖注入
- Rebudget: balance efficiency and fairness in market-based multi-core resource allocation by reallocating the budget at run time
- [target detection] yolov5 Runtong visdrone data set
- 什么是元宇宙Gamefi链游系统开发?Gamefi元宇宙NFT链游系统开发应用案例及分析
- 在华为昇腾Ascend910上复现swin_transformer
- Roson的Qt之旅#99 QML表格控件-TableView
- 方正期货网上开户靠谱吗,开户安全吗?
猜你喜欢

Frustrated Internet people desperately knock on the door of Web3

How to deploy applications on IPFs using 4everland cli

企业直播风起:目睹聚焦产品,微赞拥抱生态

Add batch delete

QT listview list display component notes

在华为昇腾Ascend910上复现swin_transformer

多租户软件开发架构

Rainbond插件扩展:基于Mysql-Exporter监控Mysql

谁动了我的内存,揭秘 OOM 崩溃下降 90% 的秘密

【目标检测】YOLOv5跑通VisDrone数据集
随机推荐
win10如何删除微软拼音输入法
Budget report ppt
In the eyes of 100 users, there are 100 QQS
Using rank to discuss the solution of linear equations / the positional relationship of three planes
为什么 4EVERLAND 是 Web 3.0 的最佳云计算平台
After 20 years of agitation, the chip production capacity has started from zero to surpass that of the United States, which is another great achievement made in China
简述redis集群的实现原理
【目标检测】YOLOv5跑通VOC2007数据集(修复版)
Mindoc makes mind map
华泰vip账户证券开户安全吗
unity 最好用热更方案卧龙 wolong
How to install govendor and open a project
MySQL view
2022 latest Beijing Construction welder (construction special operation) simulation question bank and answer analysis
Page table cache of Linux kernel source code analysis
EasyUI DataGrid control uses
ReBudget:通过运行时重新分配预算的方法,在基于市场的多核资源分配中权衡效率与公平性
The gas is exhausted! After 23 years of operation, the former "largest e-commerce website in China" has become yellow...
Who moved my memory and revealed the secret of 90% reduction in oom crash
152. 乘积最大子数组