一天一道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. Android Topeka介绍

    概述 当你已经做Android开发一段时间,并苦于进入瓶颈,这个时候阅读一些优秀App的源码是最好的学习进阶方式,前几天,邀请去参加一个Android大会,我作为其中一个演讲者,专门讲解了Androi ...

  2. 理解性能的奥秘——应用程序中慢,SSMS中快(4)——收集解决参数嗅探问题的信息

    本文属于<理解性能的奥秘--应用程序中慢,SSMS中快>系列 接上文:理解性能的奥秘--应用程序中慢,SSMS中快(3)--不总是参数嗅探的错 前面已经提到过关于存储过程在SSMS中运行很 ...

  3. SQLite Select 语句(http://www.w3cschool.cc/sqlite/sqlite-select.html)

    SQLite Select 语句 SQLite 的 SELECT 语句用于从 SQLite 数据库表中获取数据,以结果表的形式返回数据.这些结果表也被称为结果集. 语法 SQLite 的 SELECT ...

  4. BeanUtils 读取数据

    前两篇文章都是关于setProperty的,下面来说一个关于getProperty 的小案例.如下: MyClass.java package beanutils; public class MyCl ...

  5. [ExtJS5学习笔记]第二十四节 Extjs5中表格gridpanel或者表单数据后台传输remoteFilter设置

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/39667533 官方文档:http://docs.sencha.com/extjs/5. ...

  6. 【iOS 开发】Objective - C 面向对象 - 方法 | 成员变量 | 隐藏封装 | KVC | KVO | 初始化 | 多态

    一. Objective-C 方法详解 1. 方法属性 (1) OC 方法传参机制 Object-C 方法传参机制 : OC 中得参数传递都是值传递, 传入参数的是参数的副本; -- 基本类型 (值传 ...

  7. 从二进制数据流中构造GDAL可以读取的图像数据(C#)

    在上一篇博客中,讲了一下使用GDAL从文件流中构造一个GDAL可以识别的数据来进行处理.原以为这个接口在C#中没有,仔细看了下GDAL库中源码,发现C#版本也有类似的函数,下面是GDAL库中的一个C# ...

  8. ubuntu安装水星MW150US无线网卡8188eu驱动

    买了一个无线网卡插在ubuntu系统的电脑上,却不能识别出来.lsusb,可以看到下面的结果: Bus 002 Device 002: ID 0bda:8179 Realtek Semiconduct ...

  9. Ext JS 6开发实例(四) :调整主视图

    上文把主界面设置好,但是主视图因为界面的微调出现了显示问题,本文将把它调整好了. 打开app/view/main/Main.js,可以看到主视图是派生于标签面板(Ext.tab.Panel)的.在视图 ...

  10. inline内联函数

    demo //带参数的宏 #define MYFUNC(a, b) ((a) < (b) ? (a) : (b)) inline int myfunc(int a, int b) { retur ...