当前位置:网站首页>[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
边栏推荐
- 又一款装机神器
- oracle如何导出数据(oracle如何备份数据库)
- BUUCTF做题Upload-Labs记录pass-11~pass-20
- Space shooting Lesson 11: sound and music
- MobileViT:挑战MobileNet端侧霸主
- [C language brush questions] explanation of linked list application
- Algorithm interview high frequency problem solving guide [1]
- 图书馆借阅系统「建议收藏」
- MoCo V1:视觉领域也能自监督啦
- C language final review questions
猜你喜欢

protobuf 中基础数据类型的读写

DeiT:注意力Attention也能蒸馏

九鑫智能正式加入openGauss社区

Explain the camera in unity and its application

4.2 Virtual Member Functions

Random talk on GIS data (VI) - projection coordinate system

Unity foundation 6-rotation

MySQL sorts out the review content -- with mind map

Explain various coordinate systems in unity in detail
Looking at SQL optimization from the whole process of one query
随机推荐
[Zhou Zhou has a prize] cloud native programming challenge "edge container" track invites you to fight!
Use order by to sort
Read the recent trends of okaleido tiger and tap the value and potential behind it
融合数据库生态:利用 EventBridge 构建 CDC 应用
(转)冒泡排序及优化详解
Why on earth is it not recommended to use select *?
Unit editor details
C # basic 5-asynchronous
C language function program example (super complete)
protobuf 中基础数据类型的读写
Report redirect after authorized login on wechat official account_ The problem of wrong URI parameters
Alibaba cloud MSE supports go language traffic protection
Link with Bracket Sequence I(状态基多维dp)
Baklib|为什么说企业需要重视客户体验?
Interpretation of ue4.25 slate source code
New development of letinar in Korea: single lens 4.55G, light efficiency up to 10%
《软件设计师考试》易混淆知识点
[C language brush questions] explanation of linked list application
SharkTeam完成Flow生态NFT市场MatrixMarket的安全审计
C # basic 1-events and commissions