当前位置:网站首页>【题目】两数相加
【题目】两数相加
2022-07-28 19:15:00 【xiaoxigua_cs】
题目描述
给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。
请你将两个数相加,并以相同形式返回一个表示和的链表。你可以假设除了数字 0 之外,这两个数都不会以 0 开头。
输入:l1 = [2,4,3], l2 = [5,6,4]
输出:[7,0,8]
解释:342 + 465 = 807.
解决方案
方案一
在这里插入代码片
方案二
存在精度问题,目前不推荐用
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)));
}
/** * 数组转链表的方法 */
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;
}//在此处打印结点容易导致head的变化
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);
//头节点
ListNode headNode = new ListNode(Character.getNumericValue(result.charAt(0)));
//游标指针指向头结点
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;
}
/** * 数字反转 * * @param x 输入的值 * @return 翻转后的值 */
public static String reverse(long x) {
return new StringBuffer(String.valueOf(x)).reverse().toString();
}
/** * 根据链表,获取链表里面的数组 * * @param l 链表 * @return 里面的数组 */
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;
}
/** * 根据数组获取反转后的数字 * 例如[1, 2, 3] -> 321 * * @param nums 数字的集合 */
private static long getReversalNum(ArrayList<Long> nums) {
//入参校验
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;
}
}
题目来源:力扣-两数相加
边栏推荐
- New development of letinar in Korea: single lens 4.55G, light efficiency up to 10%
- Read the recent trends of okaleido tiger and tap the value and potential behind it
- JS fly into JS special effect pop-up login box
- mysql还有哪些自带的函数呢?别到处找了,看这个就够了。
- C language final review questions
- Baklib|为什么说企业需要重视客户体验?
- Unity - Fundamentals of 3D mathematics
- 2 enjoy yuan mode
- 广和通&高通物联网技术开放日成功举办
- mysql梳理复习内容--附思维导图
猜你喜欢

Explain the mobile control implementation of unity in detail

58岁安徽人,干出瑞士今年最大IPO 投资界

Explain the imported 3D model in unity

Explain mesh Collider in unity

The EMC vnx5200 fault light is on, but there is no hardware fault prompt

Tested interviewed Zuckerberg: reveal more details of four VR prototypes

JS fly into JS special effect pop-up login box

EfficientFormer:轻量化ViT Backbone

mysql梳理复习内容--附思维导图

Redis 3.0 source code analysis - data structure and object SDS list Dict
随机推荐
2 enjoy yuan mode
ntp服务器 时间(查看服务器时间)
Hangao database best practice configuration tool Hg_ BP log collection content
Seventeen year operation and maintenance veterans, ten thousand words long, speak through the code of excellent maintenance and low cost~
JS page black and white background switch JS special effect
JS fly into JS special effect pop-up login box
Unity foundation 6-rotation
Space shooting Lesson 16: props (Part 2)
C # basic 1-events and commissions
怎样搭建企业内部维基百科
Space shooting lesson 14: player life
dll反编译(反编译加密dll)
1 Introduction to command mode
全链路灰度在数据库上我们是怎么做的?
[工具类] Map的util包, 常用 实体类转化为map等操作
Observer mode, object pool
setPreviewCallbackWithBuffer的出帧效率会变低
After Europe, it entered Japan and South Korea again, and the globalization of Pico consumer VR accelerated
数据库--explain的使用
程序员最大的浪漫~