一天一道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. 微信小程序实例-摇一摇抽奖

    概述 前面我们讲了如何开始微信小程序搭建和一些组件的介绍.微信小组件和微信小程序入门 微信小程序目录 为了更好的理解小程序和小程序开发,我们首先来看一下项目的目录. 首先看下根目录下的app.json ...

  2. Eclipse编写ExtJS卡死问题 eclise js验证取消

    1. Eclipse编写ExtJS卡死问题 eclise js验证取消 近期项目用到了extjs,发现项目编译的时候特别的卡,浪费很多时间而且保存的时候还要编译,因此打算看下如何取消验证extjs.最 ...

  3. 【Netty源码学习】BootStrap

    BootStrap是客户端的启动类,其主要功能就是设置必要的参数然后启动客户端. 实现如下: Bootstrap b = new Bootstrap(); b.group(group) .channe ...

  4. 【Netty源码解析】NioEventLoop

    上一篇博客[Netty源码学习]EventLoopGroup中我们介绍了EventLoopGroup,实际说来EventLoopGroup是EventLoop的一个集合,EventLoop是一个单线程 ...

  5. 让 Google Test 出错时断点

    Google Test 缺省是出错退出. 如果最后的出错行在系统库中,那就没什么帮助. 如果是调试运行,直接退出根本就不知道哪里出错了. 后来添加了一个运行参数: --gtest_break_on_f ...

  6. Android开发学习之路--Notification之初体验

    一般当我们收到短信啊,微信啊,或者有些app的提醒,我们都会在通知栏收到一天简单的消息,然后点击消息进入到app里面,其实android中有专门的Notification的类可以完成这个工作,这里就实 ...

  7. 8.非关系型数据库(Nosql)之mongodb的应用场景

     测试脚本: Mysql测试脚本: [php] view plaincopyprint? 1.  <?php 2.  header("Content-Type:text/html; ...

  8. Android开发工具下载地址

    Android Studio: http://zdz.la/iq4zSa

  9. Sublime Text 3 使用MarkDown编写带预览的文本

    看到别人使用一个叫Markdown的标记语言来完成编码,心里就有点小激动,毕竟简短的几个符号,就可以写出如此精美的界面,实在是让人感到心旷神怡啊.于是我就在网上搜索了一些相关项的设置,于是便有了下面的 ...

  10. UNIX环境高级编程——线程私有数据

    线程私有数据(Thread-specific data,TSD):存储和查询与某个线程相关数据的一种机制. 在进程内的所有线程都共享相同的地址空间,即意味着任何声明为静态或外部变量,或在进程堆声明的变 ...