• 2.Add Two Numbers


    题目链接:https://leetcode.com/problems/add-two-numbers/description/

    题目大意:给出两个单链表,表示两个数字,这两个数字中的每一位上的数字在单链表中都倒序存放,每个结点中都含有一个非负整数,计算这两个数字的和,要求倒序插入到单链表中并返回。例子如下:

    法一(WA):利用栈,对于每一个链表,将数字取出来依次放入栈中,然后再取出存成它本来表示的数,然后将两个数相加,再将相加的结果存放进单链表中返回。这个解法比较好理解,就是转换成数学数字再计算,但是当数字长度超过int范围的时候就会发生问题,也就是int会存不下的问题。代码如下:

     1     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
     2         Stack<Integer> stack1 = new Stack<Integer>();
     3         Stack<Integer> stack2 = new Stack<Integer>();
     4         while(l1 != null) {
     5             stack1.push(l1.val);
     6             l1 = l1.next;
     7         }
     8         while(l2 != null) {
     9             stack2.push(l2.val);
    10             l2 = l2.next;
    11         }
    12         int num1 = 0, num2 = 0;
    13         while(!stack1.isEmpty()) {
    14             num1 = num1 * 10 + stack1.pop();
    15         }
    16         while(!stack2.isEmpty()) {
    17             num2 = num2 * 10 + stack2.pop();
    18         }
    19         int num = num1 + num2;
    20         ListNode l = new ListNode(num % 10);
    21         num = num / 10;
    22         ListNode tmp = null, p = l;
    23         while(num != 0) {
    24             tmp = new ListNode(num % 10);
    25             tmp.next = null;
    26             p.next = tmp;
    27             p = tmp;
    28             num = num / 10;
    29         }
    30         return l;
    31     }
    View Code

    法二:在经历上个方法的失败后,立马想到了大数加法,转而用大数加法顺利成章的做,迎刃而解,只是要处理一些细节问题,比如要考虑到最后flag不是0而是1的问题。代码如下(耗时55ms):

     1     //类似大数加法
     2     public ListNode addTwoNumbers2(ListNode l1, ListNode l2) {
     3         ListNode l = null, tmp = null, p = null;
     4         int flag = 0, num = 0;
     5         while(l1 != null && l2 != null) {
     6             if(l == null) {
     7                 num = l1.val + l2.val;
     8                 flag = num / 10;
     9                 l = new ListNode(num % 10);
    10                 l.next = null;
    11                 p = l;
    12             }
    13             else {
    14                 num = l1.val + l2.val + flag;
    15                 flag = num / 10;
    16                 tmp = new ListNode(num % 10);
    17                 tmp.next = null;
    18                 p.next = tmp;
    19                 p = tmp;
    20             }
    21             l1 = l1.next;
    22             l2 = l2.next;
    23         }
    24         while(l1 != null) {
    25             num = flag + l1.val;
    26             flag = num / 10;
    27             tmp = new ListNode(num % 10);
    28             tmp.next = null;
    29             p.next = tmp;
    30             p = tmp;
    31             l1 = l1.next;
    32         }
    33         while(l2 != null) {
    34             num = flag + l2.val;
    35             flag = num / 10;
    36             tmp = new ListNode(num % 10);
    37             tmp.next = null;
    38             p.next = tmp;
    39             p = tmp;
    40             l2 = l2.next;
    41         }
    42         if(flag != 0) {
    43             tmp = new ListNode(flag);
    44             tmp.next = null;
    45             p.next = tmp;
    46             p = tmp;
    47         }
    48         return l;
    49     }
    View Code
  • 相关阅读:
    Object-C 声明属性为什么用下划线,代码规范和编程风格
    iOS API 概述
    iOS 彻底学会使用delegate
    iOS NSNotification的使用
    L1_6 连续因子
    天梯 L1_46整除光棍
    51-Nod 1279
    UVA
    hdu 1078
    Poj 1088 滑雪 递归实现
  • 原文地址:https://www.cnblogs.com/cing/p/7747957.html
Copyright © 2020-2023  润新知