zoukankan      html  css  js  c++  java
  • leetcode 2 Add Two Numbers

    题目内容

    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.
    

    分析过程

    • 题目归类:
      链表,迭代
    • 题目分析:
      最好的情况是能够走一趟,想到使用递归可以层层深入,先计算最外的数据。然后向内传进位来计算。麻烦的是如何判断两个链的长度
      注意链之间和进位之间的关系。
    • 边界分析:
      • 空值分析
        当都为null是返回null
        当一方为null返回另一方。
      • 循环边界分析
    • 方法分析:
      • 数据结构分析
        链表需要控制什么时候指针指向null
        递归需要考虑返回的内容(当指向null时返回null,当一方为null返回另一方)
      • 状态机
      • 状态转移方程
      • 最优解
    • 测试用例构建
      [0],[0];
      null,null;

    代码实现

    /**
     * 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) {
            return addPlus(l1,l2,0);
        }
        public ListNode addPlus(ListNode l1, ListNode l2, int carry){
            if(l1==null && l2 ==null&&carry!=0){
                return new ListNode(carry);
            }else if(l1 == null && l2 == null&& carry == 0)
                return null;
            else if(l1 == null&&carry==0)
                return l2;
            else if(l1 == null&&carry!=0){
                int sum = l2.val +carry;
                l2.val = sum%10;
                l2.next = addPlus(l1,l2.next,sum/10);
                return l2;
             }else if (l2 == null&&carry==0)
                return l1;
             else if(l2 == null&&carry!=0){
                int sum = l1.val +carry;
                l1.val = sum%10;
                l1.next=addPlus(l1.next,l2,sum/10);
                return l1;
             }
            int sum = l1.val+l2.val+carry;
            
            ListNode head = new ListNode(sum%10);
            head.next = addPlus(l1.next,l2.next,sum/10);
            return head;
            
        }
    }
    

    效率提高

    使用非递归的方式可以获得更加简洁的代码,并且不需要很多if/else

    /**
     * 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) {
            ListNode cur = new ListNode(0);
            ListNode head = cur;
            int carry = 0;
            while(l1!=null||l2!=null||carry!=0){
                int sum = (l1==null?0:l1.val) + (l2==null?0:l2.val) + carry;
                cur.next = new ListNode(sum%10);
                carry = sum /10;
                cur = cur.next;
                l1= l1==null?l1:l1.next;
                l2 = l2==null?l2:l2.next;
            }
            return head.next;
        }
    }
    

    拓展问题

    Multiply Strings
    Add Binary
    Sum of Two Integers
    Add Strings
    Add Two Numbers II
    Add to Array-Form of Integer

  • 相关阅读:
    Vue笔记:使用 vuex 管理应用状态
    Vue + Element UI 实现权限管理系统(更换皮肤主题)
    Vue + Element UI 实现权限管理系统(优化登录流程)
    Vue + Element UI 实现权限管理系统(国际化实现)
    Vue笔记:使用 axios 中 this 指向问题
    Vue + Element UI 实现权限管理系统(工具模块封装)
    Vue + Element UI 实现权限管理系统(搭建开发环境)
    Vue 全家桶
    android 检测ListView滚动到的位置
    android 工具类之SharePreference
  • 原文地址:https://www.cnblogs.com/clnsx/p/12249000.html
Copyright © 2011-2022 走看看