当前位置:网站首页>【题目】两数相加
【题目】两数相加
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;
}
}
题目来源:力扣-两数相加
边栏推荐
- 【TiDB】txt文档导入数据库,这样做真的很高效
- 什么是 CI/CD? | 实现更快更好的软件交付
- Thinking and summary of R & D Efficiency
- How to modify the ID of NetApp expansion enclosure disk shelf
- Guo Mingxuan: meta contraction is conducive to the development of VR competitors, and apple XR headshow will change the industry rules
- Redis 3.0 source code analysis - data structure and object SDS list Dict
- Introduction to redis I: redis practical reading notes
- 1 Introduction to command mode
- Prometheus complete process of configuring alertmanager
- Space shooting lesson 09: elf animation
猜你喜欢
Unity foundation 1 - event execution sequence, custom events
MobileViT:挑战MobileNet端侧霸主
Tested interviewed Zuckerberg: reveal more details of four VR prototypes
Interpretation of netappp SP sensors output content
Explain various coordinate systems in unity in detail
Subcontracting loading of wechat applet
What is "security"? Volvo tells you with its unique understanding and action
Moco V1: the visual field can also be self supervised
Laser slam:logo-loam --- code compilation, installation and gazebo test
MoCo V2:MoCo系列再升级
随机推荐
查询oracle视图创建语句及如何向视图中插入数据[通俗易懂]
4.2 Virtual Member Functions
Unity foundation 2 editor expansion
MySQL修改端口号(修改mysql的端口号会有问题吗)
Using viewpager to slide through pages in fragment
Seventeen year operation and maintenance veterans, ten thousand words long, speak through the code of excellent maintenance and low cost~
Explain the imported 3D model in unity
C # basic 4-written examination question 1
Easynlp Chinese text and image generation model takes you to become an artist in seconds
Random talk on GIS data (VI) - projection coordinate system
Algorithm interview high frequency problem solving guide [1]
mysql还有哪些自带的函数呢?别到处找了,看这个就够了。
阿里云 MSE 支持 Go 语言流量防护
The 678th operation
Interesting pictures and words
Unity - Fundamentals of 3D mathematics
Zcmu--5066: dark corridor
Integrating database Ecology: using eventbridge to build CDC applications
Unity foundation 5-optimization strategy
什么是 CI/CD? | 实现更快更好的软件交付