当前位置:网站首页>力扣每日一题 06.29 两数相加
力扣每日一题 06.29 两数相加
2022-06-29 17:32:00 【一阵风R】
力扣每日一题 06.29 两数相加
当看到标题写的两数相加是不是感觉很简单,那么好,接下来我们看一下题目:
给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。
请你将两个数相加,并以相同形式返回一个表示和的链表。
你可以假设除了数字 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 = [0], l2 = [0]
输出:[0]
提示:
每个链表中的节点数在范围
[1, 100]内0 <= Node.val <= 9题目数据保证列表表示的数字不含前导零
接下来的我进入了深思。。。
经过了N小时后,有了一丢丢的思路,开始写代码

再经过几小时后,他绿了 ヾ(◍°∇°◍)ノ゙

发现有个100%,当我欣喜若狂的时候,仔细一看,提交次数超过了100%的用户,我干!!!
下面是我的垃圾代码
/**
* 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) {
ListNode header = new ListNode();
ListNode count = header;
int carryNum = 0, type;
while (true) {
type = l1.val + l2.val + carryNum;
carryNum = type / 10;
ListNode tempListNode = new ListNode(type % 10);
count.val = tempListNode.val;
tempListNode = new ListNode();
l1 = l1.next;
l2 = l2.next;
if (l1 != null || l2 != null) {
count.next = new ListNode();
count = count.next;
if (l1 == null) {
l1 = new ListNode(0);
} else if (l2 == null) {
l2 = new ListNode(0);
}
} else if (carryNum > 0 && l1 == null && l2 == null) {
count.next = new ListNode();
count = count.next;
l1 = new ListNode(0);
l2 = new ListNode(0);
}else {
break;
}
}
return header;
}
}
官方代码
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head = null, tail = null;
int carry = 0;
while (l1 != null || l2 != null) {
int n1 = l1 != null ? l1.val : 0;
int n2 = l2 != null ? l2.val : 0;
int sum = n1 + n2 + carry;
if (head == null) {
head = tail = new ListNode(sum % 10);
} else {
tail.next = new ListNode(sum % 10);
tail = tail.next;
}
carry = sum / 10;
if (l1 != null) {
l1 = l1.next;
}
if (l2 != null) {
l2 = l2.next;
}
}
if (carry > 0) {
tail.next = new ListNode(carry);
}
return head;
}
}
来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/add-two-numbers
边栏推荐
- 在供应链场景应用中,供应链管理系统扮演什么角色?
- Redis bloom filter and cuckoo filter
- 【现代信号处理第六次作业】
- Open source warehouse contribution - submit pr
- MySQL highly available cluster – MHA
- 手把手教你在windows上安装mysql8.0最新版本数据库,保姆级教学
- 自定義HandlerInterceptor攔截器實現用戶鑒權
- Online sql to CSV tool
- First batch! Tencent cloud's ability to pass the solution of the government affairs collaboration platform of the China Academy of ICT
- How to solve MySQL 1045 error in Linux
猜你喜欢

PCB板框的绘制——AD19

MySQL触发器如何创建与删除

Subgraphs in slam

腾讯云发布自动化交付和运维产品Orbit,推动企业应用全面云原生化

What are the usage scenarios for locks in MySQL

High landing pressure of "authorization and consent"? Privacy computing provides a possible compliance "technical solution"

@Difference between component and @configuration

迈动互联中标大家保险集团

What is the MySQL query view command

How to solve the 2003 error of MySQL in Linux
随机推荐
Word2vec vector model of Wiki Chinese corpus based on deep learning
How to use interrupt
The fixed assets management system enables enterprises to dynamically master assets
Shenzhen internal promotion | Shenzhen Institute of computing science recruits assistant machine learning Engineer (school recruitment)
KUKA机器人外部轴配置你一定要知道的那些知识
线段树、树状数组模板(复制粘贴确实好用)
从居家办公中感悟适配器模式 | 社区征文
Mysql高可用集群–MHA
Does MySQL support foreign keys
自定义HandlerInterceptor拦截器实现用户鉴权
跨境独立站语言unicode转希伯来语
KUKA robot external axis configuration what you must know
力扣今日题-535. TinyURL 的加密与解密
Tencent cloud released orbit, an automated delivery and operation and maintenance product, to promote enterprise applications to be fully cloud native
Naacl 2022 | distillation of machinetranslation SOTA model
Help MySQL data analysis with databend
Leetcode daily question - 535 Encryption and decryption of tinyurl
SRM系统可以为企业带来什么价值?
开源仓库贡献 —— 提交 PR
What are the project management systems suitable for small and medium-sized enterprises?