当前位置:网站首页>Leetcode solution - 02 Add Two Numbers
Leetcode solution - 02 Add Two Numbers
2022-07-03 05:58:00 【Ashley shot the sun】
Leetcode The first 02.Add Two Numbers topic , questions Medium.
One . Subject requirements
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
The main idea of the topic
Two integers are stored in two linked lists in reverse order . That is, each node of the linked list stores a digit , The first node stores bits , And so on . Sum through these two linked lists .
Two . Their thinking & Code implementation
Since it's summation , Then it's OK to traverse the two linked lists from scratch and add them , Two problems need to be paid attention to in the process of traversal calculation :
Carry problem in calculation , In addition, if there is still carry after traversal, you need to create a new node storage
There is a linked list traversal completed in advance , Then the value of subsequent traversal of the linked list is 0
Although it is medium Difficult questions , But it's not difficult to do , Just think about the calculation in the traversal process . The implementation code is as follows :
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode result = new ListNode(0);
ListNode tail = result;
// Carry or not
int isopsephy = 0;
while (l1 != null || l2 != null) {
int value1 = l1 == null ? 0:l1.val;
int value2 = l2 == null ? 0:l2.val;
int val = value1 + value2 + isopsephy;
if (val > 9) {
val = val % 10;
isopsephy = 1;
}else {
isopsephy = 0;
}
tail.val = val;
l1 = l1 == null? null: l1.next;
l2 = l2 == null? null: l2.next;
// Jingle
if (l1 == null && l2 == null) {
if (isopsephy > 0) {
tail.next = new ListNode(isopsephy);
tail = tail.next;
}
break;
}
tail.next = new ListNode(0);
tail = tail.next;
}
return result;
}
}
- Running results
Runtime: 1 ms, faster than 100.00% of Java online submissions for Add Two Numbers.
Lao tie saw this and gave a wave of praise 、 Comment on 、 How about paying attention to Sanlian
I am a AhriJ Zou classmate , Front and rear ends 、 Applet 、DevOps Are engaged in the explosion of stack engineers . The blog is constantly updated , If you think it's good , Welcome to the old tiesanlian , If it's not good, you're welcome to correct , learn from each other , Common progress .
边栏推荐
- Why is the website slow to open?
- Sophomore dilemma (resumption)
- 1. Somme des deux nombres
- Disruptor learning notes: basic use, core concepts and principles
- [teacher Zhao Yuqiang] use Oracle's tracking file
- Crontab command usage
- Deep learning, thinking from one dimensional input to multi-dimensional feature input
- BeanDefinitionRegistryPostProcessor
- Final review (Day6)
- pytorch 搭建神经网络最简版
猜你喜欢
Clickhouse learning notes (I): Clickhouse installation, data type, table engine, SQL operation
redis 无法远程连接问题。
Deep learning, thinking from one dimensional input to multi-dimensional feature input
Skywalking8.7 source code analysis (II): Custom agent, service loading, witness component version identification, transform workflow
[teacher Zhao Yuqiang] calculate aggregation using MapReduce in mongodb
[written examination question analysis] | | get [sizeof and strlen] [pointer and array] graphic explanation + code analysis
最大似然估计,散度,交叉熵
Apt update and apt upgrade commands - what is the difference?
[explain in depth the creation and destruction of function stack frames] | detailed analysis + graphic analysis
Maximum likelihood estimation, divergence, cross entropy
随机推荐
How does win7 solve the problem that telnet is not an internal or external command
Txt document download save as solution
[teacher Zhao Yuqiang] Alibaba cloud big data ACP certified Alibaba big data product system
[escape character] [full of dry goods] super detailed explanation + code illustration!
Intel's new GPU patent shows that its graphics card products will use MCM Packaging Technology
88. Merge two ordered arrays
Apache+php+mysql environment construction is super detailed!!!
Complete set of C language file operation functions (super detailed)
[teacher Zhao Yuqiang] redis's slow query log
1. 兩數之和
Redis encountered noauth authentication required
Error 1045 (28000) occurs when Linux logs in MySQL: access denied for user 'root' @ 'localhost' (using password: yes)
Bio, NiO, AIO details
Pytorch builds the simplest version of neural network
Skywalking8.7 source code analysis (I): agent startup process, agent configuration loading process, custom class loader agentclassloader, plug-in definition system, plug-in loading
Sorry, this user does not exist!
The server data is all gone! Thinking caused by a RAID5 crash
伯努利分布,二项分布和泊松分布以及最大似然之间的关系(未完成)
Final review (Day6)
[video of Teacher Zhao Yuqiang's speech on wot] redis high performance cache and persistence