当前位置:网站首页>[Topic] add two numbers
[Topic] add two numbers
2022-07-28 21:06:00 【xiaoxigua_ cs】
Title Description
Here are two for you Non empty The linked list of , Represents two nonnegative integers . Each of them is based on The reverse Stored in , And each node can only store a Numbers .
Please add up the two numbers , And returns a linked list representing sum in the same form . You can assume that in addition to the numbers 0 outside , Neither of these numbers 0 start .
Input :l1 = [2,4,3], l2 = [5,6,4]
Output :[7,0,8]
explain :342 + 465 = 807.
Solution
Scheme 1
Insert a code chip here
Option two
There is accuracy problem , Not recommended at present
public class Test {
public static void main(String[] args) {
int[] a = {
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
int[] b = {
5, 6, 4};
System.out.println(addTwoNumbers(arrayToNode(a), arrayToNode(b)));
}
/** * Array to linked list method */
public static ListNode arrayToNode(int[] arr) {
ListNode head = new ListNode(arr[0]);
ListNode other = head;
for (int i = 1; i < arr.length; i++) {
ListNode temp = new ListNode(arr[i]);
other.next = temp;
other = other.next;
}// Printing nodes here can easily lead to head The change of
return head;
}
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ArrayList<Long> nums1 = getNumsByLinkedList(l1);
long n1 = getReversalNum(nums1);
ArrayList<Long> nums2 = getNumsByLinkedList(l2);
long n2 = getReversalNum(nums2);
long sum = n1 + n2;
String result = reverse(sum);
// Head node
ListNode headNode = new ListNode(Character.getNumericValue(result.charAt(0)));
// The cursor pointer points to the header node
ListNode cursor = headNode;
for (int i = 1; i < result.length(); i++) {
cursor.next = new ListNode(Character.getNumericValue(result.charAt(i)));
cursor = cursor.next;
}
cursor = headNode;
return cursor;
}
/** * Number reversal * * @param x The value of the input * @return Flipped value */
public static String reverse(long x) {
return new StringBuffer(String.valueOf(x)).reverse().toString();
}
/** * According to the linked list , Get the array in the linked list * * @param l Linked list * @return The array inside */
private static ArrayList<Long> getNumsByLinkedList(ListNode l) {
ArrayList<Long> nums = new ArrayList<>();
while (true) {
nums.add((long) l.val);
if (l.next == null) {
break;
}
l = l.next;
}
return nums;
}
/** * Get the inverted number according to the array * for example [1, 2, 3] -> 321 * * @param nums A collection of numbers */
private static long getReversalNum(ArrayList<Long> nums) {
// Input verification
long result = 0;
int length = nums.size();
for (int i = length - 1; i >= 0; i--) {
result += nums.get(i) * Math.pow(10, i);
}
return result;
}
}
Title source : Power button - Addition of two numbers
边栏推荐
- ZCMU--5066: 黑暗长廊
- Explain in detail the rays and radiographic testing in unity
- Easynlp Chinese text and image generation model takes you to become an artist in seconds
- [tool class] util package of map, common entity classes are converted to map and other operations
- Three deletion strategies and eviction algorithm of redis
- MoCo V1:视觉领域也能自监督啦
- 一名在读研究生的自白:我为什么会沉迷于openGauss 社区?
- 第六七八次作业
- 智能家居行业发展,密切关注边缘计算和小程序容器技术
- Unity3d tutorial notes - unity initial 02
猜你喜欢

BUUCTF做题Upload-Labs记录pass-11~pass-20

Ask if you don't understand, and quickly become an advanced player of container service!

两款吾爱破解优秀软件,批量查找文本,图像视频画质增强

How to use redis to realize things and locks?

MobileViT:挑战MobileNet端侧霸主

Subcontracting loading of wechat applet

Why on earth is it not recommended to use select *?

什么是“安全感”?沃尔沃用它自己独特的理解以及行动来告诉你
![[C language brush questions] explanation of linked list application](/img/44/1de4e3f0712780680fbdc904dfe39a.png)
[C language brush questions] explanation of linked list application

速卖通测评自养号,国外环境如何搭建?需要多少成本?
随机推荐
ntp服务器 时间(查看服务器时间)
Cause analysis of restart of EMC cx4-120 SPB controller
Oracle library access is slow. Why?
Space shooting Lesson 13: explosion effect
MoCo V2:MoCo系列再升级
Subcontracting loading of wechat applet
BUUCTF做题Upload-Labs记录pass-11~pass-20
Interpretation of ue4.25 slate source code
Explain the script data transmission and notification in unity
C # basic 7-iterator and coroutine
Observer mode, object pool
[Zhou Zhou has a prize] cloud native programming challenge "edge container" track invites you to fight!
Explain mesh Collider in unity
小程序容器技术,让移动研发效率提升500%
查询oracle视图创建语句及如何向视图中插入数据[通俗易懂]
Link with Bracket Sequence I(状态基多维dp)
第六七八次作业
The average altitude is 4000 meters! We built a cloud on the roof of the world
Using viewpager to slide through pages in fragment
Prize essay solicitation | 2022 cloud native programming challenge draft activity opens