鱼C论坛

 找回密码
 立即注册
查看: 1172|回复: 0

[学习笔记] leetcode 2. Add Two Numbers

[复制链接]
发表于 2019-8-19 07:02:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. 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.

  2. You may assume the two numbers do not contain any leading zero, except the number 0 itself.

  3. Example:

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

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. *     int val;
  5. *     ListNode next;
  6. *     ListNode(int x) { val = x; }
  7. * }
  8. */

  9. import java.math.BigInteger;

  10. class Solution {
  11.    
  12.     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
  13.         
  14.         ListNode result =  new ListNode(0);
  15.         
  16.         ListNode head = result;
  17.         
  18.         BigInteger x , y;
  19.         
  20.         if(l1 == null && l2 == null){
  21.             
  22.             return null;
  23.         }
  24.         
  25.         if(l1 == null){
  26.             
  27.             return l2;
  28.         }
  29.         
  30.         if(l2 == null){
  31.             
  32.             return l1;
  33.         }
  34.         
  35.         
  36.         x = new BigInteger(reverse(get_string(l1)));
  37.         y = new BigInteger(reverse(get_string(l2)));
  38.         
  39.         x = x.add(y);
  40.         
  41.         String a = reverse(x.toString());
  42.         
  43.         int len = a.length();
  44.         
  45.         for(int i = 0; i< len; i++){
  46.             
  47.             Integer it = new Integer(a.substring(i,i+1));
  48.             
  49.             result.next = new ListNode(it.intValue());
  50.             
  51.             result = result.next;
  52.         }
  53.         
  54.         return head.next;
  55.         
  56.     }
  57.    
  58.     public String get_string(ListNode l){
  59.         
  60.         String re = "";
  61.         
  62.         while(l != null){
  63.             
  64.             re = re + l.val;
  65.             
  66.             l = l.next;
  67.         }
  68.         
  69.         return re;
  70.     }
  71.    
  72.     public String reverse(String str){
  73.         
  74.         String re = "";
  75.         
  76.         int len = str.length();
  77.         
  78.         for(int i = 0; i< len; i++){
  79.             
  80.             re = str.substring(i,i+1) + re;
  81.         }
  82.         
  83.         return re;
  84.     }
  85.    
  86. }
复制代码


12.jpg

这个code 虽然可以通过,但是效率太低了,继续优化代码.

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. *     int val;
  5. *     ListNode next;
  6. *     ListNode(int x) { val = x; }
  7. * }
  8. */

  9. class Solution {
  10.    
  11.     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
  12.         
  13.         ListNode result = new ListNode(0);
  14.         
  15.         ListNode l = l1, j = l2, cur = result;
  16.         
  17.         int digit = 0;
  18.         
  19.         while(l != null || j != null){
  20.             
  21.             int x = (l != null) ? l.val : 0;
  22.             
  23.             int y = (j != null) ? j.val : 0;
  24.             
  25.             int sum = digit + x + y;
  26.             
  27.             digit = sum / 10;
  28.             
  29.             ListNode nex = new ListNode(sum % 10);
  30.             
  31.             cur.next = nex;
  32.             
  33.             cur = cur.next;
  34.             
  35.             if(l != null){
  36.                
  37.                 l = l.next;
  38.             }
  39.             
  40.             if(j != null){
  41.                
  42.                 j = j.next;
  43.             }
  44.         }
  45.         
  46.         if(digit > 0 ){
  47.             
  48.             ListNode nex = new ListNode(digit);
  49.             
  50.             cur.next = nex;
  51.             
  52.             cur = cur.next;
  53.         }
  54.         
  55.         return result.next;
  56.     }
  57.    
  58. }
复制代码


13.jpg

nice job!

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 09:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表