【一天一道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 ...
随机推荐
- android Spinner控件详解
Spinner提供了从一个数据集合中快速选择一项值的办法.默认情况下Spinner显示的是当前选择的值,点击Spinner会弹出一个包含所有可选值的dropdown菜单,从该菜单中可以为Spinner ...
- SpringMVC源码分析--文件上传
SpringMVC提供了文件上传的功能,接下来我们就简单了解一下SpringMVC文件上传的开发及大致过程. 首先需要在springMVC的配置文件中配置文件上传解析器 <bean id=&qu ...
- activiti实战系列 排他网关(ExclusiveGateWay)
流程图 12.2:部署流程定义+启动流程实例 12.3:查询我的个人任务 12.4:完成我的个人任务 说明: 1) 一个排他网关对应一个以上的顺序流 2) 由排他网关流出的顺序流都有个 ...
- J2EE规范标准
J2EE是一个很大的平台体系,提供了很多服务.程序接口.协议等.这么庞大的体系必须要由一系列的标准进行规范,不然将会一片混乱.通过这些规范好的接口来开发程序将会使程序更加强壮.更加有生命力.总的来说, ...
- [ExtJS5学习笔记]第二十九节 sencha ext js 5.1.0中动态更换皮肤主题
本文地址:http://blog.csdn.net/sushengmiyan/article/details/42016107 本文作者:sushengmiyan ------------------ ...
- lager_transform未定义错误
lager_transform未定义错误rebar编译时报错:D:\server\six>d:/tools/rebar/rebar.cmd compile==> mysql (compil ...
- Android项目开发填坑记-Fragment的onAttach
背景 现在Android开发多使用一个Activity管理多个Fragment进行开发,不免需要两者相互传递数据,一般是给Fragment添加回调接口,让Activity继承并实现. 回调接口一般都写 ...
- Linux系统编程-----进程fork()
在开始之前,我们先来了解一些基本的概念: 1. 程序, 没有在运行的可执行文件 进程, 运行中的程序 2. 进程调度的方法: 按时间片轮转 先来先服务 短时间优先 按优先级别 3. 进程的状态: 就绪 ...
- Android的RadioButton和checkBox的用法-android学习之旅(十九)
RadioButton和checkBox简介 单选按钮(RadioButton)和复选框(CheckBox)都继承了Button,因此属性的设置和Button差不多,只是加了一个android:che ...
- 如何使用EasyUI显示表格界面
还记得前面有篇博客叫---使用TT模板+mvc+wcf实现简单查询,这篇博文的末尾,小编贴了一张查询出来的结果图,那么这篇博客的中新来了,如何使用EasyUI显示出表格样式的界面,以前学习CS的时候, ...