zoukankan      html  css  js  c++  java
  • LeetCode 2 Add Two Numbers(链表操作)

    题目来源:https://leetcode.com/problems/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

    题目要求:给出两个链表l1和l2,将相对应的元素相加,如果产生进位则保留个位,相应的十位加在后面的对应元素上.

    解体思路:

    首先判断链表是否为空,若l1空,直接返回l2,相反返回l2

    然后找到l1和l2链表最短的一个长度min_len,进入循环

    循环过程中,为了减少空间的开销,可以将链表l1作为最终的结果链表,只需要改变l1的最大容量为l1+l2即可,另外相加过程中需要一个整型temp变量来保存进位.

    循环结束之后,判断l1或l2空否(之前计算最短链表时可以做标记),然后将不空的链表中其他元素全部放入结果链表中即可.

    提交代码:

     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 
    10 class Solution {
    11 public:
    12     ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
    13         // Start typing your C/C++ solution below
    14         // DO NOT write int main() function
    15 //        ListNode *pResult = NULL;
    16 //        ListNode **pCur = &pResult;
    17 
    18         ListNode rootNode(0);
    19         ListNode *pCurNode = &rootNode;
    20         int a = 0;
    21         while (l1 || l2)
    22         {
    23             int v1 = (l1 ? l1->val : 0);
    24             int v2 = (l2 ? l2->val : 0);
    25             int temp = v1 + v2 + a;
    26             a = temp / 10;
    27             ListNode *pNode = new ListNode((temp % 10));
    28             pCurNode->next = pNode;
    29             pCurNode = pNode;
    30             if (l1)
    31                 l1 = l1->next;
    32             if (l2)
    33                 l2 = l2->next;
    34         }
    35         if (a > 0)
    36         {
    37             ListNode *pNode = new ListNode(a);
    38             pCurNode->next = pNode;
    39         }
    40         return rootNode.next;
    41     }
    42 };

    下面给出数组解决的代码,具体过程相同,只是数据存储结构不同:

     1 #include <bits/stdc++.h>
     2 #define MAX 1000010
     3 
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     int n1,n2;
     9     while(~scanf("%d %d",&n1,&n2))
    10     {
    11         int *a=new int[n1+n2+1];
    12         int *b=new int[n2];
    13         for(int i=0;i<n1;i++)
    14             scanf("%d",&a[i]);
    15         for(int i=0;i<n2;i++)
    16             scanf("%d",&b[i]);
    17         int minlen=min(n1,n2);
    18         int maxlen=n1+n2-minlen;
    19         int temp=0;
    20         for(int i=0;i<minlen;i++)
    21         {
    22             a[i]=a[i]+b[i]+temp;
    23             if(a[i]>=10)
    24             {
    25                 temp=a[i]/10;
    26                 a[i]=a[i]%10;
    27             }
    28         }
    29         for(int j=minlen;j<maxlen;j++)
    30         {
    31                 if(maxlen==n1)
    32                     a[j]=a[j];
    33                 else
    34                     a[j]=b[j];
    35         }
    36         for(int i=0;i<maxlen;i++)
    37             printf("%d ",a[i]);
    38         printf("
    ");
    39     }
    40 }

      

  • 相关阅读:
    两个单向链表的第一个公共节点
    c字符输出
    堆排序
    归并排序
    LR中,URL -based script与HTML -based script区别
    loadrunner文本检查点
    Loadrunner集合点Rendezvous知识
    连接池(Connection Pool)技术
    lucene 查询 (转载)
    Lucene + Hadoop 分布式搜索运行框架 Nut 1.0a9转自http://www.linuxidc.com/Linux/2012-02/53113.htm
  • 原文地址:https://www.cnblogs.com/zpfbuaa/p/5049623.html
Copyright © 2011-2022 走看看