码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 力扣(LeetCode)2. 两数相加(C++\C)


    模拟

    模拟加法运算,设置进位数 t t t , t = ( l 1 t=(l1 t=(l1-> v a l + l 2 val+l2 val+l2-> v a l + t ) % 10 val+t)\%10 val+t)%10 即为当前位上的数, t / 10 t/10 t/10 即是进位数。

    设置哑结点,便于操作头结点。
    模拟上述操作,最后返回哑结点的后继。

    代码展示
    class Solution {
    public:
        ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
            int t = 0;//进位
            ListNode *dummy = new ListNode(0);
            auto cur = dummy;
            while(l1||l2){
                if(l1) t+=l1->val,l1 = l1->next;
                if(l2) t+=l2->val,l2 = l2->next;
                cur->next = new ListNode(t%10);//当前位的数
                cur = cur->next;
                t/=10;//进位数
            }
            if(t) cur->next = new ListNode(t);
            return dummy->next;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    //同时遍历两个链表
    struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
        struct ListNode* cur = (struct ListNode*)calloc(1, sizeof(struct ListNode));//声明一个链表,存储答案
        struct ListNode* dummyhead = (struct ListNode*)calloc(1, sizeof(struct ListNode));//哑结点,用于存储cur的值
        dummyhead = cur;
        int next = 0;//是否进位
        while (l1 && l2) {
            int sum = l1->val + l2->val + next;
            cur->next = (struct ListNode*)calloc(1, sizeof(struct ListNode));
            cur->next->val = sum % 10;
            if (sum >= 10) {
                next = 1;
            }
            else {
                next = 0;
            }
            cur = cur->next;
            l2 = l2->next, l1 = l1->next;//所有链表后移一位
        }
        while (l2) {//遍历结束,l2非空
            int sum = l2->val + next;
            cur->next = (struct ListNode*)calloc(1, sizeof(struct ListNode));
            cur->next->val = sum % 10;
            if (sum >= 10) {
                next = 1;
            }
            else {
                next = 0;
            }
            cur = cur->next;
            l2 = l2->next;
        }
        while (l1) {//遍历结束,l2非空
            int sum = l1->val + next;
            cur->next = (struct ListNode*)calloc(1, sizeof(struct ListNode));
            cur->next->val = sum % 10;
            if (sum >= 10) {
                next = 1;
            }
            else {
                next = 0;
            }
            cur = cur->next;
            l1 = l1->next;
        }
        if(next){
            cur->next = (struct ListNode*)calloc(1, sizeof(struct ListNode));
            cur->next->val = 1;
        }
        return dummyhead->next;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    博主致语

    理解思路很重要!
    欢迎读者在评论区留言,作为日更博主,看到就会回复的。

    AC

    AC

    复杂度分析
    1. 时间复杂度: O ( m a x ( m , n ) ) O(max(m,n)) O(max(m,n)), n n n 是 l 1 l1 l1 的长度, m m m 是 l 2 l2 l2 的长度,同时遍历 l 1 , l 2 l1,l2 l1,l2 , 一次遍历得到答案。
    2. 空间复杂度: O ( 1 ) O(1) O(1),除去保存答案所用空间,没有使用额外的线性空间。
  • 相关阅读:
    内网渗透系列之Pivoting跳板攻击与自动路由的配置以及使用ProxyChains进行代理扫描并获取内网服务器权限
    Java接口的相关知识
    仅30行代码,实现一个搜索引擎(1.0版)
    Leo赠书活动-05期 【打造敏捷测试团队】文末送书5本
    【初阶数据结构】——顺序表详解(C描述)
    Spring MVC组件之HandlerMapping
    leetcode-240:搜索二维矩阵 II
    [SpringBoot系列]SSMP整合小项目
    python3数学计算(四则运算、数学函数)整除、取余、幂运算、平方根、阶乘、对数、三角函数、平均值、标准差、积分、微分等、python数学计算
    探索图像分割技术:使用 OpenCV 的分水岭算法
  • 原文地址:https://blog.csdn.net/Innocence02/article/details/127906506
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号