[Leetcode] Add two numbers 两数之和
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
题意:求以链表形式表示的两非负整数之和,高位在后。
思路:联想数组形式的计算,以一个变量carry为对应位置的和,carry%10为当前值,carry/10为进位。在数组里,直接在长度较大的数组更新值就行,但是链表的长度没法直接获得。所以出现了问题一:如何保存数据。所以针对这个问题有两种解法,但求和的思想还是不变。问题二、若是最高位有进位,怎么办?这种情况下,只能新建一个结点,然后赋值。
方法一:开辟一个新的链表,每次都是将对应的值赋给新开辟的结点。这就遇到循环条件的问题,若是l1&&l2则会遇到有其中一个链表遍历完时,剩下的链表的进位情况,若剩下的结点值全是9,即存在进位的可能则可能有需要另一个while循环,所以这里为代码的简洁还是用 l1||l2 。还有一点值得注意的是,对当前结点的取值一定要在当前结点存在的情况下,不然会造成错误。代码如下:
/**
* 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)
{
if(l1==NULL) return l2;
if(l2==NULL) return l1; int carry=;
ListNode *nList=new ListNode(-);
ListNode *pre=nList; while(l1||l2)
{
if(l1)
{
carry+=l1->val;
l1=l1->next;
}
if(l2)
{
carry+=l2->val;
l2=l2->next;
} pre->next=new ListNode(carry%);
carry/=;
pre=pre->next;
} if(carry==)
pre->next=new ListNode(); return nList->next;
}
};
方法二:参考数组的做法,先找出较长的,然后更新较长链表上结点的值。参考LeetCode上的讨论。
/**
* 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 flag=;
int sum=;
ListNode *pointer1=l1;
ListNode *pointer2=l2;
ListNode *result=NULL;
while(pointer1&&pointer2)
{
pointer1=pointer1->next;
pointer2=pointer2->next;
}
if(pointer1==NULL)
result=l2;
else
result=l1; ListNode *nList=result; while(l1||l2)
{
sum=(l1?l1->val:)+(l2?l2->val:)+flag;
flag=sum/;
result->val=sum%;
l1?l1=l1->next:l1;
l2?l2=l2->next:l2;
result->next?result=result->next:result; //值得注意必须先判断存在,
} //思考,若存在进位,和下面的代码会产生什么后果
if(flag==)
{
result->next=new ListNode();
} return nList;
}
};
[Leetcode] Add two numbers 两数之和的更多相关文章
- Leetcode:0002(两数之和)
LeetCode:0002(两数之和) 题目描述:给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表.你可以假设除了数字 0 之外,这两 ...
- Leetcode(1)两数之和
Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...
- [LeetCode] 1. Two Sum 两数之和
Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode Golang实现 1. 两数之和
1. 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这 ...
- Leetcode(一)两数之和
1.两数之和 题目要求: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重 ...
- leetcode每日一题——两数之和
题目: 两数之和 难度: 简单 描述: 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 解法: class Solutio ...
- LeetCode刷题 - (01)两数之和
题目描述 给定一个整数数组nums和一个目标值target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元 ...
- LeetCode题解001:两数之和
两数之和 题目 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个 ...
随机推荐
- Discuz论坛搜索下拉框插件openSug
Discuz!只需安装openSug插件即可获得带有“搜索框提示”功能的搜索框,让您的Discuz搜索更便捷! 下载:https://www.opensug.org/faq/.../opensug.d ...
- PHP计算两个时间戳之间间隔时分秒
/功能:计算两个时间戳之间相差的日时分秒//$begin_time 开始时间戳//$end_time 结束时间戳function timediff($begin_time,$end_time){ if ...
- ELK 分布式日志实战
一. ELK 分布式日志实战介绍 此实战方案以 Elk 5.5.2 版本为准,分布式日志将以下图分布进行安装部署以及配置. 当Elk需监控应用日志时,需在应用部署所在的服务器中,安装Filebeat ...
- Django自带后台使用配置
参考官网地址:https://docs.djangoproject.com/en/1.11/ref/contrib/admin/ ,本文章值是介绍简单配置,如果需要详细内容可以查阅官方文档 自动管理界 ...
- ruby 数据类型Range
范围(Range)无处不在:a 到 z. 0 到 9.等等.Ruby 支持范围,并允许我们以不同的方式使用范围: 作为序列的范围 作为条件的范围 作为间隔的范围 作为序列的范围 (1..5) #==& ...
- mysql学习第四天(高级查询)
-- 第七章-- 1.查询入职日期最早和最晚的日期select min(hiredate),max(hiredate)from emp -- 2.查询职位以SALES开头的所有员工平均工资,最低工资, ...
- LeetCode:26. Remove Duplicates from Sorted Array(Easy)
1. 原题链接 https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/ 2. 题目要求 给定一个已 ...
- vs13发布web程序 iis上
一.配置iis 1,找到控制面板--程序--启用或关闭Windows功能 2,从列表中选择Internet Infomation Services,并且把相应的功能条目勾选上,如果不清楚,可以全部选中 ...
- python中判断输入是否为数字(包括浮点数)
1.当num确定为数字后 num=123.4print(isinstance(num,float))#判断是否为浮点数 print(isinstance(num,int))#判断是否为整数 2.当nu ...
- 30分钟 带你浅入requirejs源码
因为最近项目想现实一个单页功能,用的是react ,然后看了一下react route,挖槽 gzip后16k? 然后我简单写了一个纯单页(不支持多页的单页,所有入口都经过rewrite跑到index ...