zoukankan      html  css  js  c++  java
  • leetcode-2

    给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。

    如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。

    您可以假设除了数字 0 之外,这两个数都不会以 0 开头。

    示例:

    输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
    输出:7 -> 0 -> 8
    原因:342 + 465 = 807

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/add-two-numbers
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummyHead = new ListNode(0);
        ListNode p = l1, q = l2, curr = dummyHead;
        int carry = 0;
        while (p != null || q != null) {
            int x = (p != null) ? p.val : 0;
            int y = (q != null) ? q.val : 0;
            int sum = carry + x + y;
            carry = sum / 10;
            curr.next = new ListNode(sum % 10);
            curr = curr.next;
            if (p != null) p = p.next;
            if (q != null) q = q.next;
        }
        if (carry > 0) {
            curr.next = new ListNode(carry);
        }
        return dummyHead.next;
    }

    时间复杂度是max(m, n)

    这道题不能简单的转成整型数相加,会溢出。既然题目就是链表,考察点肯定也在链表这个数据结构的应用上。还有如果做的多的话会发现,递归是很多算法题的解题方法,它很细节却又不用注重细节,好用。

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            int l1val = 0;
            int l2val = 0;
    
            
            if (l1==null){l1val =0;}
            else {l1val = l1.val;}
            
            if (l2==null){l2val =0;}
            else {l2val = l2.val;}
    
            if ((l1val + l2val)>=10) {
    
                if (l1.next !=null ){l1.next.val++;}
                else if (l2.next !=null){l2.next.val++;}
            }
            
            ListNode rtnNode = new ListNode( (l1val + l2val)%10);
    
            if (l1.next==null && l2.next==null ){
                if ((l1val + l2val)>=10){
                    rtnNode.next = new ListNode(1);
                }
                else {rtnNode.next =null;}
                }
            else if (l1.next ==null && l2.next !=null){
                rtnNode.next =  addTwoNumbers(new ListNode(0), l2.next);
            }
            else if (l1.next != null && l2.next ==null){
                rtnNode.next =  addTwoNumbers(l1.next, new ListNode(0));
            }
    
            else {rtnNode.next = addTwoNumbers(l1.next, l2.next);}
         
               return rtnNode; 
    
            
        }
    }
    一个没有高级趣味的人。 email:hushui502@gmail.com
  • 相关阅读:
    Java实现 LeetCode 833 字符串中的查找与替换(暴力模拟)
    Java实现 LeetCode 833 字符串中的查找与替换(暴力模拟)
    Java实现 LeetCode 833 字符串中的查找与替换(暴力模拟)
    Java实现 LeetCode 832 翻转图像(位运算)
    Java实现 LeetCode 832 翻转图像(位运算)
    Java实现 LeetCode 832 翻转图像(位运算)
    Java实现 LeetCode 831 隐藏个人信息(暴力)
    Java实现 LeetCode 831 隐藏个人信息(暴力)
    Java实现 LeetCode 831 隐藏个人信息(暴力)
    how to use automapper in c#, from cf~
  • 原文地址:https://www.cnblogs.com/CherryTab/p/12015836.html
Copyright © 2011-2022 走看看