2. Add Two Numbers[M]两数相加
题目
You are given two non-empty linked lists representing two non-negative integers. 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.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
思路
主要是考察链表的求和,由于链表中存放的是逆序的数字,所以两个数的个位数已经对齐,不用考虑对齐问题。其次要考虑到进位的问题:
- 当前位数字:sum %10
- 进位:sum / 10
C++
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* result = new ListNode(-1);
ListNode* aux = result; //辅助,用来处理中间链表
int carry = 0;
while(l1 != nullptr || l2 != nullptr || carry != 0){
//这种方法使得当其中一个链表为空时,也能作加法,从而能够同时对两个链表进行循环
int a = l1 ? l1->val : 0; //判断链表当前是否为空,如果为空,则值为0
int b = l2 ? l2->val : 0;
int sum = a + b + carry;
carry = sum / 10; //进位用取模来获得
aux->next = new ListNode(sum%10); //通过取模消除进位的影响
aux = aux ->next;
if(l1 != nullptr )
l1 = l1->next;
if(l2 != nullptr )
l2 = l2->next;
}
return result->next;
}
Python
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
result = ListNode(-1)
aux = result
carry = 0
while(l1 or l2 or carry):
a = l1 and l1.val or 0
b = l2 and l2.val or 0
sum = a + b + carry
carry = sum / 10
aux.next = ListNode (sum % 10)
aux = aux.next
if(l1):
l1 = l1.next
if(l2):
l2 = l2.next
return result.next
2. Add Two Numbers[M]两数相加的更多相关文章
- 445 Add Two Numbers II 两数相加 II
给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表.你可以假设除了数字 0 之外,这两个数字都不会以零开头.进阶:如果输入链表 ...
- [leetcode]445. Add Two Numbers II 两数相加II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
- LeetCode 445. Add Two Numbers II (两数相加 II)
题目标签:Linked List 题目给了我们两个 数字的linked list,让我们把它们相加,返回一个新的linked list. 因为题目要求不能 reverse,可以把 两个list 的数字 ...
- [CareerCup] 18.1 Add Two Numbers 两数相加
18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我 ...
- [Swift]LeetCode2. 两数相加 | Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- [Swift]LeetCode445. 两数相加 II | Add Two Numbers II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
- LeetCode 445. 两数相加 II(Add Two Numbers II)
445. 两数相加 II 445. Add Two Numbers II 题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个 ...
- LeetCode 2. 两数相加(Add Two Numbers)
2. 两数相加 2. Add Two Numbers 题目描述 You are given two non-empty linked lists representing two non-negati ...
- LeetCode 2. 两数相加(Add Two Numbers)
题目描述 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入: ...
随机推荐
- 异步lambda表达式
- BZOJ1060: [ZJOI2007]时态同步(树形dp 贪心)
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3285 Solved: 1286[Submit][Status][Discuss] Descript ...
- Android Toolbar使用及Fragment中的Toolbar处理
Toolbar作为ActionBar使用介绍 本文介绍了在Android中将Toolbar作为ActionBar使用的方法.并且介绍了在Fragment和嵌套Fragment中使用Toolbar作为A ...
- 错误:Camera录制视频(6.0错误),5.1正常,7.1正常 (java.lang.RuntimeException: start failed.at android.media.MediaRecorder.native_start(Native Method))
Process: com.example.mycamera2, PID: 24086 java.lang.RuntimeException: start failed. at android.medi ...
- RedHat/CentOS 大文件拆分及合并与md5验证
[root@tdh55 mnt]# cd /opt/[root@tdh55 opt]# ll -h-rw-r--r--. 1 root root 7.5G May 12 11:19 TDH-Image ...
- spring的HandlerMapping
handerlMapping意思是处理器映射,是把请求的url地址与方法进行映射,如SimpleUrlHandlerMapping.
- laravel 运行migrate报错 1071 Specified key was too long
转自:https://segmentfault.com/a/1190000008416200 laravel运行命令migrate时报错: 1071 Specified key was too lo ...
- PHP做的简单计算器
使用php做的简易计算器 能够进行+,-,*,/运算. 如下图 <?php if (isset($_POST['button'])) { $num1 = $_POST['num1']; $num ...
- 06006_redis数据存储类型——String
1.概述 (1)字符串类型是Redis中最为基础的数据存储类型,它在Redis中是二进制安全的,这意味着该类型可以接受任何格式的数据,如JPEG图像数据或Json对象描述信息等: (2)在Redis中 ...
- yii AR 模式操作
Bat::find() ; //返回查询实例 Bat::find()->one() //返回一条数据 Bat::find()->all(); //返回所有数据 Bat::find()-&g ...