zoukankan      html  css  js  c++  java
  • [Leetcode] 2.Add Two Numbers(List To Long,模拟)

    本题题意是指将两个数倒序存储在链表中,再将两数之和同样存储在链表中输出。

    我最开始的思路是将每一位相加,再考虑是否进位,但这时就需要考虑一些情况,比较麻烦。

    于是我决定采取另一种在网上新学到的方法:这个方法就是将链表中的数字串起来,当做一个long,例如2->4->5,可以根据题目具体要求转化成long型的542,再做后续的操作,就很容易了。

     1 /**
     2  * Definition for singly-linked list.
     3  * struct ListNode {
     4  *     int val;
     5  *     ListNode *next;
     6  *     ListNode(int x) : val(x), next(NULL) {}
     7  * };
     8  */
     9 class Solution
    10 {
    11 public:
    12     long ListToLong(ListNode* l)
    13     {
    14         long res = 0;
    15         long tem = 1;
    16         while(l)
    17         {
    18             res = res + l->val * tem;
    19             tem = tem * 10;
    20             l = l->next;
    21         }
    22         return res;
    23     }
    24     long get_n(long num)
    25     {
    26         long x = 10;
    27         long n = 1;
    28         while(num >= x)
    29         {
    30             n ++;
    31             x = x * 10;
    32         }
    33         return n;
    34     }
    35     void add(ListNode* l1, ListNode* l2)
    36     {
    37         while(l1->next)
    38         {
    39             l1 = l1->next;
    40         }
    41         l1->next = l2;
    42     }
    43     ListNode* LongToList(long num)
    44     {
    45         ListNode* res = new ListNode(-1);
    46         int n = get_n(num);
    47         //cout<<"n = "<<n<<endl;
    48         int x = 0;
    49         for(int i = 0;i < n;i ++)
    50         {
    51             x = num % 10;
    52             num = num / 10;
    53             ListNode* node = new ListNode(x);
    54             add(res,node);
    55         }
    56         return res->next;
    57     }
    58     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
    59     {
    60         long num1 = ListToLong(l1);
    61         long num2 = ListToLong(l2);
    62         //cout<<"num1 = "<<num1<<" num2 = "<<num2<<endl;
    63         long num3 = num1 + num2;
    64         //cout<<"num3 = "<<num3<<endl;
    65         ListNode* res = LongToList(num3);
    66         return res;
    67     }
    68 };

    但上面的代码提交后的结果很让我无语。。。见下图

    只有两个示例没有通过。。。将long改成long long依旧不能通过,应该是特意添加的这两个示例,这种算法看来只能做到这步了。

    于是我不得不回到最开始的思路,下面是AC代码

     1 /**
     2  * Definition for singly-linked list.
     3  * struct ListNode {
     4  *     int val;
     5  *     ListNode *next;
     6  *     ListNode(int x) : val(x), next(NULL) {}
     7  * };
     8  */
     9 class Solution
    10 {
    11 public:
    12     int M = 0;//表示进位
    13     ListNode* res = new ListNode(0);//结果链表
    14     int flag = 0;
    15     void add(ListNode* l1, ListNode* l2)
    16     {
    17         while(l1->next)
    18         {
    19             l1 = l1->next;
    20         }
    21         l1->next = l2;
    22     }
    23     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
    24     {
    25         ListNode* temp = new ListNode(0);
    26         if(l1 == NULL && l2 == NULL)
    27             return res->next;
    28         else if(l1 == NULL)
    29         {
    30             temp->val = temp->val + M + l2->val;
    31             if(temp->val >= 10)
    32             {
    33                 temp->val = temp->val - 10;
    34                 M = 1;
    35             }
    36             else
    37             {
    38                 M = 0;
    39             }
    40             ListNode* e1 = new ListNode(temp->val);
    41             add(res,e1);
    42             if(M && (l2->next == NULL))
    43             {
    44                 ListNode* e3 = new ListNode(1);
    45                 add(res,e3);
    46             }
    47             addTwoNumbers(NULL,l2->next);
    48             return res->next;
    49         }
    50         else if(l2 == NULL)
    51         {
    52             temp->val = temp->val + M + l1->val;
    53             if(temp->val >= 10)
    54             {
    55                 temp->val = temp->val - 10;
    56                 M = 1;
    57             }
    58             else
    59             {
    60                 M = 0;
    61             }
    62             ListNode* e2 = new ListNode(temp->val);
    63             add(res,e2);
    64             if(M && (l1->next == NULL))
    65             {
    66                 ListNode* e4 = new ListNode(1);
    67                 add(res,e4);
    68             }
    69             addTwoNumbers(l1->next,l2);
    70             return res->next;
    71         }
    72         else
    73         {
    74             int x = l1->val + l2->val + M;
    75             if(x >= 10)
    76             {
    77                 x = x - 10;
    78                 M = 1;
    79             }
    80             else
    81             {
    82                 M = 0;
    83             }
    84             ListNode* Node = new ListNode(x);
    85             add(res,Node);
    86             if(l1->next == NULL && l2->next == NULL && M == 1)
    87             {
    88                 ListNode* e = new ListNode(1);
    89                 add(res,e);
    90             }
    91             else
    92             {
    93                 addTwoNumbers(l1->next,l2->next);
    94             }
    95             return res->next;
    96         }
    97     }
    98 };
  • 相关阅读:
    python3 接口测试 一般方法
    python2 接口测试一般方法.
    Postman 官网教程,重点内容,翻译笔记,
    Unity3D --对撞机/碰撞器 介绍
    MyBatis之配置文件与操作
    Eclipse连接mysql数据库jdbc下载(图文)
    MyBatis概述和环境的搭建
    样式加载不出来,浏览器控制台报错:Resource interpreted as Stylesheet but transferred with MIME type text/html
    bootstrap之navbar
    JSP中的内置标记(JSP动作)和 EL表达式
  • 原文地址:https://www.cnblogs.com/lca1826/p/6357748.html
Copyright © 2011-2022 走看看