一天一道leetcode系列

(一)题目:

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

题目意思: 输入两个倒序的多位数,输出它们的和。

(二)代码实现:

一看到这个题,为了图简便,直接转换成int相加,然后转成链表,结果是Memory Limit Exceeded 提示超出内存限制

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        int a=0,b=0;
        while(l1) {
            a=a*10+l1->val;
            l1=l1->next;
        }
        while(l2) {
            b=b*10+l2->val;
            l2=l2->next;
        }
        int temp = a+b;
        int temp_m = temp%10;
        temp = temp/10;
        ListNode* head = new ListNode(temp_m);
        ListNode* p = head;
        while(temp/10)
        {
            temp_m = temp%10;
            ListNode* next = new ListNode(temp_m);
            p->next = next;
            p=p->next;
        }
        return head;
    }
};

无奈,只能老老实实得按加法原则来求和了。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* p = l1->next;
        ListNode* q = l2->next;
        bool jwflag = false;//进位标志位
        int temp = l1->val + l2->val;
        if(temp>=10) jwflag = true;
        ListNode* head = new ListNode(temp%10);
        ListNode* m = head;
        while(p && q)
        {
            if(jwflag) {temp = p->val+q->val +1;jwflag = false;}
            else temp = p->val+q->val;
            ListNode* ltemp = new ListNode(temp%10);
            if(temp>=10) jwflag = true;//处理进位
            m->next = ltemp;
            m = ltemp;
            p=p->next;
            q=q->next;
        }
        while(!p&&q) //p为空,q非空
        {
            if(jwflag) {temp = q->val +1;jwflag = false;}
            else temp = q->val;
            ListNode* ltemp = new ListNode(temp%10);
            if(temp>=10) jwflag = true;
            m->next = ltemp;
            m = ltemp;
            q = q->next;
        }
        while(p&&!q) //q为空,p非空
        {
            if(jwflag) {temp = p->val +1;jwflag = false;}
            else temp = p->val;
            ListNode* ltemp = new ListNode(temp%10);
            if(temp>=10) jwflag = true;
            m->next = ltemp;
            m = ltemp;
            p = p->next;
        }
        //处理最后一位的进位
        if(jwflag)
        {
            ListNode* ltemp = new ListNode(1);
            jwflag = false;
            m->next = ltemp;
            m = m->next;
        }
        return head;

    }
};

结果:Accepted。

【一天一道leetcode】 #2 Add Two Numbers的更多相关文章

  1. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  2. LeetCode:1. Add Two Numbers

    题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...

  3. [LeetCode] 445. Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  4. LeetCode 面试:Add Two Numbers

    1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  5. LeetCode #002# Add Two Numbers(js描述)

    索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...

  6. [Leetcode Week15] Add Two Numbers

    Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...

  7. [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现

    [LeetCode] Add Two Numbers 两个数字相加   You are given two non-empty linked lists representing two non-ne ...

  8. [LeetCode] 2. Add Two Numbers 两个数字相加

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  9. LeetCode之Add Two Numbers

    Add Two Numbers 方法一: 考虑到有进位的问题,首先想到的思路是: 先分位求总和得到 totalsum,然后再将totalsum按位拆分转成链表: ListNode* addTwoNum ...

  10. LeetCode 2. add two numbers && 单链表

    add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...

随机推荐

  1. Android5.0特性ToolBar

    >Toolbar是什么?大概说一下它的官方介绍.Toolbar是应用的内容的标准工具栏,`可以说是Actionbar的升级版`,两者不是独立关系,要使用Toolbar还是得跟ActionBar扯 ...

  2. FORM内置系统函数

    abort_query;                                                    停止查询的执行 add_group_column(record grou ...

  3. SQL Server 执行计划操作符详解(3)——计算标量(Compute Scalar)

    接上文:SQL Server 执行计划操作符详解(2)--串联(Concatenation ) 前言: 前面两篇文章介绍了关于串联(Concatenation)和断言(Assert)操作符,本文介绍第 ...

  4. UNIX网络编程——尝试探索基于Linux C的网卡抓包过程

     抓包首先便要知道经过网卡的数据其实都是通过底层的链路层(MAC),在Linux系统中我们获取网卡的数据流量其实是直接从链路层收发数据帧.至于如何进行TCP/UDP连接本文就不再赘述(之前的一段关于w ...

  5. 删除表中重复行SQL

    delete from table_name a where rowid < (select max(rowid) from table_name b where a.col1 = b.col1 ...

  6. Swift快速给Cocoa库内置类添加便捷初始化器

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) Cocoa中的NSShadow类默认没有我们需要的实例方法,为 ...

  7. android ndk之hello world

    前言:Android NDK r7及以上的版本已经集成了Cygwin编译环境,也就是说,我们完全可以抛弃庞大的Cygwin了. r6及以下版本,也可以抛弃几个G的完整版,使用精简过的Mini-Cygw ...

  8. UE4联机烘焙

    联机烘焙就是为了利用多台电脑解决烘焙效率的问题 1.UE4的烘焙工具在安装目录下的\Engine\Binaries\DotNET,比如我这里是E:\UnrealEngine-release\Engin ...

  9. 高仿腾讯QQ即时通讯IM项目

    前言:其实这个项目早就开发完成了,在本人的github上,本来没打算写成博客的形式,因为一个项目要写出来要花很久,但是最近看到很多 人在我的github上download后随意发布到网上,本来上传到g ...

  10. 【移动开发】SharedPreferences的兼容版本

    public class SharedPreferencesCompat { private static final String TAG = SharedPreferencesCompat.cla ...