• 【LeetCode】445. Add Two Numbers II


    You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first 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.

    Follow up:
    What if you cannot modify the input lists? In other words, reversing the lists is not allowed.

    Example:

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

    题解:

    Solution 1

      加和是从尾部开始的,而链表又只能从前开始遍历,那么这种单向的遍历顺序就及其适合用queue或者stack来辅助。显然此题用到的是stack。

     1 class Solution {
     2 public:
     3     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
     4         stack<int> s1, s2;
     5         while (l1) {
     6             s1.push(l1->val);
     7             l1 = l1->next;
     8         }
     9         while (l2) {
    10             s2.push(l2->val);
    11             l2 = l2->next;
    12         }
    13         int sum = 0;
    14         ListNode* pre = nullptr;
    15         while (!s1.empty() || !s2.empty() || sum) {
    16             if (!s1.empty()) {
    17                 sum += s1.top(); 
    18                 s1.pop();
    19             }
    20             if (!s2.empty()) {
    21                 sum += s2.top(); 
    22                 s2.pop();
    23             }
    24             ListNode *node = new ListNode(sum % 10);
    25             node->next = pre;
    26             pre = node;
    27             sum /= 10;
    28         }
    29         return pre;
    30     }
    31 };

    Solution 2

      设计的很巧妙,无需开辟存储空间。转自 Grandyang。用right记录目前最右第一个值不是9的元素。用cur遍历链表。如果cur遍历时有进位, 那么right自增1,且right到cur(左开右闭)之间的由于都是9(因为right指向的是目前最右第一个值不是9的元素),所以此区间内的元素值全部置0。

     1 class Solution {
     2 public:
     3     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
     4         int len1 = getLength(l1), len2 = getLength(l2);
     5         if (len1 < len2) 
     6             swap(l1, l2);
     7         ListNode* dummy = new ListNode(0);
     8         ListNode* cur = dummy, *right = dummy;
     9         int diff = abs(len1 - len2);
    10         
    11         while (diff) {
    12             cur->next = new ListNode(l1->val);
    13             cur = cur->next;
    14             if (cur->val != 9)
    15                 right = cur;
    16             l1 = l1->next;
    17             --diff;
    18         }
    19         while (l1) {
    20             int sum = l1->val + l2->val;
    21             cur->next = new ListNode(sum % 10);
    22             cur = cur->next;
    23             if (sum > 9) {
    24                 sum %= 10;
    25                 ++right->val;
    26                 right = right->next;
    27                 while (right!= cur) {
    28                     right->val = 0;
    29                     right = right->next;
    30                 }
    31             } 
    32             if (sum != 9)
    33                 right = cur;
    34             l1 = l1->next;
    35             l2 = l2->next;
    36         }
    37         
    38         return dummy->val == 0 ? dummy->next : dummy;
    39     }
    40     int getLength(ListNode* head) {
    41         if (!head)
    42             return 0;
    43         int len = 0;
    44         while (head) {
    45             ++len;
    46             head = head->next;
    47         }
    48         return len;
    49     }
    50 };
  • 相关阅读:
    mysql外键和连表操作
    数据库的操作
    进程之select和epoll
    jwt的应用生成token,redis做储存
    为什么前后端分离不利于seo
    redis的bitmap
    lnmp环境的nginx的tp5配置
    虚拟机安装cenos7后ifcfg看网卡无inet地址掩码等信息
    rsa加密
    hydra命令
  • 原文地址:https://www.cnblogs.com/Atanisi/p/8846020.html
Copyright © 2020-2023  润新知