zoukankan      html  css  js  c++  java
  • 简单的两数之和再次乱入<< Add Two Numbers >>

    请看题目描述
    
    

    You are given two linked lists representing two non-negative numbers. 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.

    
    

    Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
    Output: 7 -> 0 -> 8

     1//Definition for singly-linked list.
     2     class ListNode
     3     {
     4         public int val;
     5         public ListNode next;
     6         public ListNode(int x)
     7         { 
     8             val = x; 
     9         }
    
    10  } 
     分析:
      这个两数相加不难,只是两个正整数都放在了链表里面了而已。还有一点是,整数的每一位数字都是放过来存放的,那么这样就明显降低了难度了,实现起来就像我们小学的时候列树式计算两个数的和,从低位加起,和与10的余数放在本位,和与10的商产生高位进位。在本题只需从两个链表的表头开始相加,用一个存放计算结果的链表的对应位置存放两个链表对应位置的整数和进位和除以10的余数,并用临时变量存放进位,用于加入下一次的两个整数之和中。直到两个链表中长的链表也遍历完了,那么结果链表也就出来了。结果链表的整数的各位数字也是反序的。

      从前面的分析来看我们可以使用递归和非递归两种算法实现。
      C#参考实现如下:  
     递归实现:
     1        public ListNode AddTwoNumbers(ListNode l1, ListNode l2)
     2    {
     3             return AddTwoNumbers(l1, l2, 0);
     4    }
    5 public ListNode AddTwoNumbers(ListNode l1, ListNode l2 , int carry) 6 { 7 if(l1 == null && l2 == null && carry ==0) 8 { 9 return null; 10 } 11 ListNode result = new ListNode(0); 12 int sum = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
    13 result.val = sum % 10; 14 ListNode more = AddTwoNumbers( l1 == null ? null : l1.next , 15 l2 == null ? null :l2.next, 16 sum >= 10 ? 1 : 0 ); 17 result.next = more; 18 return result; 19 }
     
    非递归实现:
     1  public ListNode AddTwoNumbers2(ListNode l1, ListNode l2)
     2 {
     3              ListNode node = new ListNode(0);
     4              ListNode result = node;
     5             
     6              int carry = 0;
     7              while(l1 != null || l2 != null || carry != 0)
     8       {
     9                  int  sum =( l1 ==null ? 0 : l1.val )+( l2 == null ? 0 : l2.val )+ carry;
    10                  l1 =( l1 ==null ? null : l1.next);
    11                  l2 =( l2 == null ? null : l2.next);
    12 
    13                 node.next = new ListNode(sum % 10 );
    14                 node = node.next;
    15 
    16                  carry = sum / 10;
    17        }
    18              return result.next;
    19  }
     
     总结:这个题目不难只要静下心来就可以想出来,涉及的细节问题也不多。那么如果把题目改一下,改成两个链表中存放正整数的每一位数字是正向存放的,又该如何实现呢?注意:链表是单向的。这时就要注意两个整数的位数长短了。我们可以这样实现:a)先比较两个链表的长度并用零填充较短的链表  b)递归结果加到首部
      

                                   

  • 相关阅读:
    Bipolar transistor boosts switcher's current by 12 times
    Use a microcontroller to design a boost converter
    Simple dc/dc converter increases available power in dual-voltage system
    Single transistor provides short-circuit protection
    Circuit level-shifts ac signals
    Circuit translates I2C voltages
    Lower dc/dc-converter ripple by using optimum capacitor hookup
    Add current boost to a USB charger
    Make a DAC with a microcontroller's PWM timer
    Get buck-boost performance from a boost regulator
  • 原文地址:https://www.cnblogs.com/lrh-xl/p/5265272.html
Copyright © 2011-2022 走看看