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

    题目: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.


    思路:一开始觉得这题不难,不就是直接将两个链表的数都取出来求和,然后再构造一个新链表将每一位再插回去,写出如下代码
     1 package leetcodeTest;
     2 import java.util.*;
     3 public class addTwoNumbersClass {
     4     public static void main(String[]args){
     5     int []a={9};
     6     int[]b={1,9,9,9,9,9,9,9,9,9};
     7     ListNode l1=creatList(a);
     8     ListNode l2=creatList(b);
     9       ListNode l=  addTwoNumbers( l1, l2);
    10       while(l!=null){
    11           System.out.println(l.val);
    12           l=l.next;
    13       }
    14     }
    15 
    16     public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    17         if(l1==null&&l2==null) return null;
    18         int a=0;
    19         long sum1=l1.val,sum2=l2.val,sum=0;
    20         int m=0,n=0;//m,n代表L1,L2的长度
    21         int b=0;
    22         while(l1!=null){
    23             sum1+=a*(long)(Math.pow(10,m));
    24             l1=l1.next;
    25             m++;
    26             if(l1==null)break;
    27             a=l1.val;
    28         }
    29         while(l2!=null){
    30             sum2+=b*((long)(Math.pow(10,n)));
    31             l2=l2.next;
    32             n++;
    33             if(l2==null)break;
    34             b=l2.val;
    35         }
    36         ListNode s = new ListNode(0);
    37         ListNode r=s;
    38         sum=sum1+sum2;
    39         if(sum==0)return new ListNode(0);
    40         while(sum!=0) {
    41             int p = (int) (sum % 10);
    42             ListNode t=new ListNode(p);
    43             s.next = t;
    44             s = s.next;
    45             sum = sum / 10;
    46         }
    47         return r.next;
    48     }
    49 
    50 public static ListNode creatList(int[] x){
    51         //创建链表
    52     ListNode q = new ListNode(0);
    53     ListNode w=q;
    54     for(int i=0;i<x.length;i++) {
    55         ListNode p = new ListNode(x[i]);
    56         q.next = p;
    57         q=q.next;
    58     }
    59     return  w.next;
    60 }
    61 
    62 
    63 
    64 
    65     public static class ListNode {
    66         int val;
    67         ListNode next;
    68     ListNode(int x) { val = x; }
    69   }
    70 }

    然而我还是太年轻了,就算用上了long整形,测试数据的链表长度直接达到了60多位后,如果直接转化成数求和的话,long 也不够用这就说明思路不对

    再仔细观察一下,发现其就是一个简单的向右进位问题,取两链表的当前位直接相加,若无进位就直接将其放入新链表节点中,若有进位则将需要进位的数加入到下一位相加的数中,然后取10的余放入当前位的链表节点中,然后就行了。Your runtime beats 87.03 % of java submissions

     1 package leetcodeTest;
     2 import java.util.*;
     3 public class addTwoNumbersClass {
     4     public static void main(String[]args){
     5     int []a={2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,9};
     6     int[]b={5,6,4,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,9,9,9,9};
     7 
     8     ListNode l1=creatList(a);
     9     ListNode l2=creatList(b);
    10       ListNode l=  addTwoNumbers( l1, l2);
    11       while(l!=null){
    12           System.out.println(l.val);
    13           l=l.next;
    14       }
    15     }
    16 
    17     public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    18         if(l1==null&&l2==null) return null;
    19         ListNode s = new ListNode(0);
    20         ListNode r=s;
    21         int m=0;
    22         while(l1!=null&&l2!=null){
    23             int a=l1.val;
    24             int b=l2.val;
    25             int sum=a+b+m;
    26             m=sum/10;//向右进位的数
    27             int n=sum%10;//当前位
    28             ListNode t=new ListNode(n);
    29             s.next=t;
    30             l1=l1.next;
    31             l2=l2.next;
    32             s=s.next;
    33         }
    34         while(l1==null&&l2!=null){
    35             int b=l2.val;
    36             int sum=b+m;
    37             m=sum/10;
    38             int n=sum%10;
    39             ListNode t=new ListNode(n);
    40             s.next=t;
    41             l2=l2.next;
    42             s=s.next;
    43         }
    44         while(l1!=null&&l2==null){
    45             int a=l1.val;
    46             int sum=a+m;
    47             m=sum/10;
    48             int n=sum%10;
    49             ListNode t=new ListNode(n);
    50             s.next=t;
    51             l1=l1.next;
    52             s=s.next;
    53         }
    54         if(m!=0){
    //若l1,l2都为空时还有进位,则需要将该进位新加入链表中
    55 ListNode t=new ListNode(m); 56 s.next=t; 57 } 58 return r.next; 59 } 60 61 public static ListNode creatList(int[] x){ 62 //创建链表 63 ListNode q = new ListNode(0); 64 ListNode w=q; 65 for(int i=0;i<x.length;i++) { 66 ListNode p = new ListNode(x[i]); 67 q.next = p; 68 q=q.next; 69 } 70 return w.next; 71 } 72 73 74 75 76 public static class ListNode { 77 int val; 78 ListNode next; 79 ListNode(int x) { val = x; } 80 } 81 }

     ac后,学习了一下排名前面的代码,可以看这个代码,它通过将l1或l2为null后将其值直接赋0,免去了我的代码当中对l1==null和l2==null时的分开讨论,写的很简洁,很巧妙!

     1 class Solution {
     2     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
     3         ListNode l3 = new ListNode(0);
     4         ListNode p = l1, q = l2, current = l3;
     5         int carry = 0;
     6         while (p != null || q != null) {
     7             int x = (p == null) ? 0 : p.val;
     8             int y = (q == null) ? 0 : q.val;
     9             int sum = x + y + carry;
    10             carry = sum/10;
    11             current.next = new ListNode(sum%10);
    12             current = current.next;
    13             if(p!=null) p = p.next;
    14             if(q!=null) q = q.next;
    15         }
    16         if (carry > 0) {
    17             current.next = new ListNode(carry);
    18             current = current.next;
    19         }
    20         return l3.next;
    21     }
    22 }
  • 相关阅读:
    CF 256C Furlo and Rublo and Game【博弈论,SG函数】
    opengl笔记——OpenGL好资料备忘
    SQL SERVER 2012/2014 链接到 SQL SERVER 2000的各种坑
    ORACLE数据库对比表结构
    SSRS Reports 2008性能优化案例二
    Linux LVM学习总结——扩展卷组VG
    MySQL备份还原——AutoMySQLBackup介绍
    mysqldump: Got error: 1142: SELECT, LOCK TABLES command denied to user 'root'@'localhost' for table 'accounts' when using LOCK TABLES
    MySQL备份还原——mysqldump工具介绍
    ORACLE查看数据文件包含哪些对象
  • 原文地址:https://www.cnblogs.com/pathjh/p/9093592.html
Copyright © 2011-2022 走看看