当前位置:网站首页>Leetcode top 100 question 2 Add two numbers
Leetcode top 100 question 2 Add two numbers
2022-07-01 05:25:00 【weixin_ forty-one million two hundred and sixty-two thousand on】


public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int[] num1=ListToArray(l1);
int[] num2=ListToArray(l2);
int i1=arrayToInt(num1);
int i2=arrayToInt(num2);
int result=i1+i2;
int[] numResult=IntToArray(result);
return headAdd(numResult);
}
public int[] ListToArray(ListNode l){
ArrayList<Integer> num=new ArrayList<Integer>();
int length=0;
while(l!=null){
num.add(l.val);
l=l.next;
length++;
}
int[] result=new int[length];
for(int i=length-1,j=0;i>=0;i--,j++){
result[i]=num.get(j);
}
return result;
}
// Returns a reverse order int,[1,2,3]=321
public int arrayToInt(int[] num){
int result=0;
for(int i=num.length-1,l=num.length;i>=0;i--,l--){
result+=num[i]*Math.pow(10,l-1);
}
return result;
}
public int[] IntToArray(int value){
String str=String.valueOf(value);
int[] intArray = new int[str.length()];
for (int i = 0; i < str.length(); i++) {
// Traverse str Add each digit as intArray
Character ch = str.charAt(i);
intArray[i] = Integer.parseInt(ch.toString());
}
return intArray;
}
public ListNode headAdd(int[] num){
ListNode head=new ListNode(num[num.length-1],null);
ListNode l=head;
for(int i=num.length-2;i>=0;i--){
l.next=new ListNode(num[i],null);
l=l.next;
}
return head;
}
}
The train of thought is
1, Apply for an array of linked list lengths , Assign the value of the linked list to the array .
2, Convert these two arrays to int
3, Add two numbers
4, Convert the result to an array
5, Traversal through the array from tail to head , Use the tail interpolation method to build the linked list
边栏推荐
- Global and Chinese market for kitchen range hoods 2022-2028: Research Report on technology, participants, trends, market size and share
- And search: the suspects (find the number of people related to the nth person)
- Unity project experience summary
- Global and Chinese market of paper machine systems 2022-2028: Research Report on technology, participants, trends, market size and share
- Unity drags and modifies scene camera parameters under the editor
- Principle, technology and implementation scheme of data consistency in distributed database
- eBPF Cilium实战(2) - 底层网络可观测性
- Txncoordsender of cockroachdb distributed transaction source code analysis
- Implementation of distributed lock
- 了解 JVM 中几个相关问题 — JVM 内存布局、类加载机制、垃圾回收
猜你喜欢

Summary of spanner's paper

LevelDB源码分析之LRU Cache

Daily question -leetcode1175- permutation of prime numbers - Mathematics

使用 Nocalhost 开发 Rainbond 上的微服务应用

Set集合详细讲解

Vmware workstation network card settings and three common network modes

And search: the suspects (find the number of people related to the nth person)

基于TI DRV8424驱动步进电机实现调速和行程控制

0xc000007b应用程序无法正常启动解决方案(亲测有效)

Spanner 论文小结
随机推荐
More than one file was found with OS independent path ‘lib/armeabi-v7a/libyuv.so‘.
Use of STM32 expansion board temperature sensor and temperature humidity sensor
积分商城游戏能够给商家带来什么?怎么搭建积分商城?
复制宝贝提示材质不能为空,如何解决?
Things generated by busybox
1076 Forwards on Weibo
液压滑环的特点讲解
Lock free concurrency of JUC (leguan lock)
Intelligent operation and maintenance: visual management system based on BIM Technology
Txncoordsender of cockroachdb distributed transaction source code analysis
Global and Chinese market of protection circuit modules 2022-2028: Research Report on technology, participants, trends, market size and share
CentOS 7使用yum安装PHP7.0
[Yugong series] February 2022 Net architecture class 005 ABP vNext Net core web application getting started configuration
Global and Chinese market of broadband amplifiers 2022-2028: Research Report on technology, participants, trends, market size and share
3D建模與處理軟件簡介 劉利剛 中國科技大學
Use and principle of AQS related implementation classes
Copy baby prompt: material cannot be empty. How to solve it?
busybox生成的东西
Print stream and system setout();
HCIP Day13