前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode笔记:445. Add Two Numbers II

LeetCode笔记:445. Add Two Numbers II

作者头像
Cloudox
发布2021-11-23 16:14:17
1760
发布2021-11-23 16:14:17
举报
文章被收录于专栏:月亮与二进制月亮与二进制

问题:

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Follow up: What if you cannot modify the input lists? In other words, reversing the lists is not allowed. Example: Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 8 -> 0 -> 7

大意:

给出两个非空的链表表示的非负整数。最高位的数在第一个,每个节点都只包含一个数字。将两个数相加并作为链表返回。 你可以假设两个数字不包含0开头的数字,除非就是0。 进阶: 你能不改变原有链表吗?也就是说不反转链表。 例子: 输入: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4) 输出: 7 -> 8 -> 0 -> 7

思路:

这道题的意思就是由链表表示的两个大数字相加得出结果,比如例子中就是 7243 + 564 = 7807

我们相加两个数字必须要从最低位开始加,所以肯定得先遍历链表知道分别有多少位,还得一位位的往高位加起来,所以我们用两个数组分别记录两个链表各个位的数字和两个链表的位数,然后都从最后一位往前一位位的做加法,注意会有进位,加法过程中还要注意存在某个数位数更多,没有加完的情况,当然也要考虑最后有没有最高一位进位的情况。

在计算结果的时候还是要用数组一位位的记录,最后再换成链表。

这种做法的时间复杂度是O(n),还是比较快的,不过是以空间的消耗为代价。

代码(Java):

代码语言:javascript
复制
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int[] arr1 = new int[100];
        int index = 0;
        ListNode newl = l1;
        while (newl != null) {
            arr1[index] = newl.val;
            newl = newl.next;
            index ++;
        }
        arr1 = Arrays.copyOfRange(arr1, 0, index);
        
        int[] arr2 = new int[100];
        index = 0;
        newl = l2;
        while (newl != null) {
            arr2[index] = newl.val;
            newl = newl.next;
            index ++;
        } 
        arr2 = Arrays.copyOfRange(arr2, 0, index);
        
        int n1 = arr1.length-1;
        int n2 = arr2.length-1;
        int[] result = new int[100];
        index = 0;
        int flag = 0;
        while (n1 >= 0 && n2 >= 0) {
            int sum = arr1[n1] + arr2[n2] + flag;
            if (sum > 9) {
                flag = 1;
                sum = sum % 10;
            } else flag = 0;
            
            result[index] = sum;
            index ++;
            n1 --;
            n2 --;
        }
        while (n1 >= 0) {
            int sum = arr1[n1] + flag;
            if (sum > 9) {
                flag = 1;
                sum = sum % 10;
            } else flag = 0;
            
            result[index] = sum;
            index ++;
            n1 --;
        }
        while (n2 >= 0) {
            int sum = arr2[n2] + flag;
            if (sum > 9) {
                flag = 1;
                sum = sum % 10;
            } else flag = 0;
            
            result[index] = sum;
            index ++;
            n2 --;
        }
        if (flag == 1) result[index] = 1;
        else index --;
        
        if (index == -1) return null;
        ListNode head = new ListNode(result[index]);
        ListNode nextNode = head;
        while (index > 0) {
            index --;
            ListNode newNext = new ListNode(result[index]);
            nextNode.next = newNext;
            nextNode = newNext;
        }
        
        return head;
    }
}

他山之石:

代码语言:javascript
复制
public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        Stack<Integer> s1 = new Stack<Integer>();
        Stack<Integer> s2 = new Stack<Integer>();
        
        while(l1 != null) {
            s1.push(l1.val);
            l1 = l1.next;
        };
        while(l2 != null) {
            s2.push(l2.val);
            l2 = l2.next;
        }
        
        int sum = 0;
        ListNode list = new ListNode(0);
        while (!s1.empty() || !s2.empty()) {
            if (!s1.empty()) sum += s1.pop();
            if (!s2.empty()) sum += s2.pop();
            list.val = sum % 10;
            ListNode head = new ListNode(sum / 10);
            head.next = list;
            list = head;
            sum /= 10;
        }
        
        return list.val == 0 ? list.next : list;
    }
}

这个做法差不多,不过是把数组的存储换成栈的存储,刚好符合从最后一位开始加的情况,比要多预留位数的数组更节省空间。

合集:https://github.com/Cloudox/LeetCode-Record

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017/11/23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 问题:
  • 大意:
  • 思路:
  • 代码(Java):
  • 他山之石:
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档