【一天一道leetcode】 #2 Add Two Numbers
一天一道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的更多相关文章
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- LeetCode:1. Add Two Numbers
题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- LeetCode 面试:Add Two Numbers
1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- LeetCode #002# Add Two Numbers(js描述)
索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...
- [Leetcode Week15] Add Two Numbers
Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...
- [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现
[LeetCode] Add Two Numbers 两个数字相加 You are given two non-empty linked lists representing two non-ne ...
- [LeetCode] 2. Add Two Numbers 两个数字相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- LeetCode之Add Two Numbers
Add Two Numbers 方法一: 考虑到有进位的问题,首先想到的思路是: 先分位求总和得到 totalsum,然后再将totalsum按位拆分转成链表: ListNode* addTwoNum ...
- LeetCode 2. add two numbers && 单链表
add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...
随机推荐
- Sqoop-1.4.6 Merge源码分析与改造使其支持多个merge-key
Sqoop中提供了一个用于合并数据集的工具sqoop-merge.官方文档中的描述可以参考我的另一篇博客Sqoop-1.4.5用户手册. Merge的基本原理是,需要指定新数据集和老数据集的路径,根据 ...
- 前端CSS技术全解(一)
一.概述 1)用HTML完成样式工作 哪个标签有哪个属性难以记忆 需求变更影响较大(例如像修改成功法则以下的文字颜色需要修改4个地方) <h1 align="center"& ...
- Scala:函数式编程之下划线underscore
http://blog.csdn.net/pipisorry/article/details/52913548 python参考[python函数式编程:apply, map, lambda和偏函数] ...
- Android Multimedia框架总结(二十)MediaCodec状态周期及Codec与输入/输出Buffer过程(附实例)
转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/53183718 前言:前面几节都是 ...
- Activity平移动画
Activity平移动画 效果图 添加动画文件 在res下添加anim文件夹,在anim下添加几个动画文件,分别是进入和退出的动画时间和移动距离,属性很简单,一看就懂,不磨叽了. tran_next_ ...
- 给定整数a1、a2、a3、...、an,判断是否可以从中选出若干个数,使得它们的和等于k(k任意给定,且满足-10^8 <= k <= 10^8)。
给定整数a1.a2.a3.....an,判断是否可以从中选出若干个数,使得它们的和等于k(k任意给定,且满足-10^8 <= k <= 10^8). 分析:此题相对于本节"寻找满 ...
- 抽屉效果的实现(DrawerLayout和SlidingMenu的对比)
在做谷歌电子市场的时候用的是DrawerLayout实现的抽屉效果,在新闻客户端的时候用的是开源框架SlidingMenu来实现的,总的来说,各有个的优点,侧滑(开源框架)实现的效果更好,但是Draw ...
- 剑指Offer——乐视笔试题+知识点总结
剑指Offer--乐视笔试题+知识点总结 情景回顾 时间:2016.9.19 15:10-17:10 地点:山东省网络环境智能计算技术重点实验室 事件:乐视笔试 总体来说,乐视笔试内容体量不算少, ...
- FFmpeg源代码简单分析:常见结构体的初始化和销毁(AVFormatContext,AVFrame等)
===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...
- 下载android4.4.2源码全过程(附已下载的源码)
今天在下载andriod源码,特来与大家分享一下我的经验.当然,网上教下载源码的教程较多,本文主要针对在GFW下下载源码出现的各种问题的解决方法. 1.首先安装下载客户端git , curl. 命令如 ...