记录-23.7.10

时隔N天…
记录一下几个类似的题目

LC 两数相加

链表逆序存储,所以直接按照顺序进行计算就好了,就是在相加过程中要注意进位的问题。
然后在head前面可以用一个虚拟头节点,这样可以避免一些边界问题。

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* dummyHead = new ListNode();
        ListNode* cur = dummyHead;
        int carry = 0;
        while(l1 || l2){
            int val = carry;
            if(l1){
                val += l1->val;
                l1 = l1->next;
            }
            if(l2){
                val += l2->val;
                l2 = l2->next;
            }
            carry = val>9 ? 1:0;
            val = val%10;
            ListNode* temp = new ListNode(val);
            cur->next = temp;
            cur = cur->next;
        }
        if(carry>0){
            ListNode* temp = new ListNode(carry);
            cur->next = temp;
            cur = cur->next;
        }
        return dummyHead->next;
    }
};

LC445 两数相加2

这里的数字是顺序存储的,所以需要做一个逆序的过程。因为加法运算是从个位开始计算的,但是链表顺序存储的话在表头的部分是高位,如果从高位开始进行加法运算就会不好计算进位。
进行逆序计算有两种方法:

  1. 链表反转
  2. 通过stack等其他数据结构进行结果的存储,然后进行加法运算

这题和剑指 Offer II 025. 链表中的两数相加是相同的题目。

链表反转

这里就是单纯得先对链表进行反转,然后使用上面两数相加的方法。

class Solution {
public:
   ListNode* reverse(ListNode* head){
       ListNode* pre = NULL;
       ListNode* cur = head;
       ListNode* temp;
       while(cur){
           temp = cur->next;
           cur->next = pre;
           pre = cur;
           cur = temp;
       }
       return pre;
   }
   ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
       l1 = reverse(l1);
       l2 = reverse(l2);
       ListNode* dummyHead = new ListNode();
       ListNode* cur = dummyHead;
       int carry = 0;
       while(l1 || l2){
           int val = carry;
           if(l1){
               val += l1->val;
               l1 = l1->next;
           }
           if(l2){
               val += l2->val;
               l2 = l2->next;
           }
           carry = val/10;
           val = val%10;
           cur->next = new ListNode(val);
           cur = cur->next;
       }
       if(carry>0){
           cur->next = new ListNode(carry);
           cur = cur->next;
       }
       dummyHead->next = reverse(dummyHead->next);
       return dummyHead->next;
   }
};

使用stack

用两个栈将两个链表的内容存入,然后加法计算后再输出到链表节点。因为栈就是一个先进后出的过程,相当于逆序了。

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        stack<int> stk1;
        stack<int> stk2;
        while(l1){
            stk1.push(l1->val);
            l1 = l1->next;
        }
        while(l2){
            stk2.push(l2->val);
            l2 = l2->next;
        }
        ListNode* dummyHead = new ListNode();
        int carry = 0;
        while(!stk1.empty() || !stk2.empty()){
            int val = carry;
            if(!stk1.empty()){
                val += stk1.top();
                stk1.pop();
            }
            if(!stk2.empty()){
                val += stk2.top();
                stk2.pop();
            }
            carry = val/10;
            val = val%10;
            ListNode* cur = new ListNode(val);
            cur->next = dummyHead->next;
            dummyHead->next = cur;
        }
        if(carry>0){
            ListNode* cur = new ListNode(carry);
            cur->next = dummyHead->next;
            dummyHead->next = cur;
        }
        return dummyHead->next;
    }
};
WARNING: Discarding https://files.pythonhosted.org/packages/c8/18/631398e45c109987f2d8e57f3adda161cc5ff2bd8738ca830c3a2dd41a85/gevent-21.12.0.tar.gz#sha256=f48b64578c367b91fa793bf8eaaaf4995cb93c8bc45860e473bf868070ad094e (from https://pypi.org/simple/gevent/) (requires-python:>=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5). Command errored out with exit status 1: /Users/didi/code/doraemon-api/.venv/bin/python /Users/didi/code/doraemon-api/.venv/lib/python3.9/site-packages/pip/_vendor/pep517/in_process/_in_process.py get_requires_for_build_wheel /var/folders/cf/1231crpx4hsf3vf_6wtb6ybw0000ks/T/tmpan2lf0yh Check the logs for full command output. ERROR: Could not find a version that satisfies the requirement gevent==21.12.0 (from versions: 0.9.2, 0.9.3, 0.10.0, 0.11.0, 0.11.1, 0.11.2, 0.12.0, 0.12.1, 0.12.2, 0.13.0, 0.13.1, 0.13.2, 0.13.3, 0.13.4, 0.13.5, 0.13.6, 0.13.7, 0.13.8, 1.0, 1.0.1, 1.0.2, 1.1a1, 1.1a2, 1.1b1, 1.1b2, 1.1b3, 1.1b4, 1.1b5, 1.1b6, 1.1rc1, 1.1rc2, 1.1rc3, 1.1rc4, 1.1rc5, 1.1.0, 1.1.1, 1.1.2, 1.2a1, 1.2a2, 1.2.0, 1.2.1, 1.2.2, 1.3a1, 1.3a2, 1.3b1, 1.3b2, 1.3.0, 1.3.1, 1.3.2, 1.3.2.post0, 1.3.3, 1.3.4, 1.3.5, 1.3.6, 1.3.7, 1.4.0, 1.5a1, 1.5a2, 1.5a3, 1.5a4, 1.5.0, 20.4.0, 20.5.0, 20.5.1, 20.5.2, 20.6.0, 20.6.1, 20.6.2, 20.9.0, 20.12.0, 20.12.1, 21.1.0, 21.1.1, 21.1.2, 21.8.0, 21.12.0, 22.8.0, 22.10.1, 22.10.2, 23.7.0, 23.9.0.post1, 23.9.1, 24.2.1, 24.10.1, 24.10.2, 24.10.3, 24.11.1, 25.4.1, 25.4.2, 25.5.1) ERROR: No matching distribution found for gevent==21.12.0
06-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值