zoukankan      html  css  js  c++  java
  • LeeCode(No2

    LeeCode是一个有意思的编程网站,主要考察程序员的算法

    第二题

    You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order 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.

    Example

    Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
    Output: 7 -> 0 -> 8
    Explanation: 342 + 465 = 807.

    算是一个中度难度的编程题,刚开始考虑转化为整数来加和,但任何类型的整数其实都是有上限的,所以此方法舍弃。

    最后自己提交的source如下,想法也是中规中矩吧

    /**
     * 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) {
          if(l1 == null || l2 == null) {
                return null;
            }
            // 为了不影响传入参数,设定游标
            ListNode cursor1 = l1;
            ListNode cursor2 = l2;
    
            // 结果
            ListNode result = new ListNode(0);
            ListNode cursorResult = result;
    
            // 进位
            int carry = 0;
            // 每位加和
            int sum;
            // 暂存游标的值
            int cursorVal1 = cursor1.val;
            int cursorVal2 = cursor2.val;
    
            do{
                sum = cursorVal1 + cursorVal2 + carry;
                // 是否有进位
                if(sum >= 10) {
                    carry = 1;
                } else {
                    carry = 0;
                }
                cursorResult.next = new ListNode(sum % 10);
                cursorResult = cursorResult.next;
                // 如果其中一个游标为空,也就是是说有链表已经遍历完
                if(cursor1 != null && cursor1.next != null) {
                    cursor1 = cursor1.next;
                    cursorVal1 = cursor1.val;
                } else {
                    cursor1 = null;
                    cursorVal1 = 0;
                }
                if(cursor2 != null && cursor2.next != null) {
                    cursor2 = cursor2.next;
                    cursorVal2 = cursor2.val;
                } else {
                    cursor2 = null;
                    cursorVal2 = 0;
                }
    
            } while (cursor1 != null || cursor2 != null);
    
            // 最后一个进位为算进去的话
            if(carry == 1) {
                cursorResult.next = new ListNode(1);
            }
    
            // 去掉首个元素
            return result.next;
        }
    }

     提交后性能表现结果如下:

    虽然击败了93%的人,但是还有7%的人性能优于我的,看了下官方给出的答案,整体思路是一样的,而且它将x,y的声明放在循环内部。这可能也是统计的不精确吧

    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;
    }

     时间复杂度和空间复杂度如下:

    Complexity Analysis

    • Time complexity : O(max(m, n))O(max(m,n)). Assume that mm and nn represents the length of l1l1 and l2l2 respectively, the algorithm above iterates at most max(m, n)max(m,n) times.

    • Space complexity : O(max(m, n))O(max(m,n)). The length of the new list is at most max(m,n) + 1max(m,n)+1.

    参考:

    https://leetcode.com/problems/add-two-numbers

    ---栖息之鹰(一个外表懒洋洋的内心有激情的程序员) 此博客为笔者原著,转载时请注明出处,谢谢!
  • 相关阅读:
    【大数据】Hadoop常用启动命令
    Codeforces Round #695 (Div. 2)
    Codeforces Round #668 (Div. 2)
    Codeforces Round #666 (Div. 2)
    Educational Codeforces Round 94 (Rated for Div. 2)
    Sum Queries? CodeForces
    New Year and Old Subsequence CodeForces
    Gym
    huntian oy HDU
    The Boss on Mars HDU
  • 原文地址:https://www.cnblogs.com/roostinghawk/p/8151297.html
Copyright © 2011-2022 走看看