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 之外,这两个数字都不会以零开头. 示例: 输入: ...
随机推荐
- Codeforces Round #454
Masha and Bears Tic-Tac-Toe Shockers Seating of Students Party Power Tower Reverses
- WinRAR 5.60 无广告正式版
首先明确WinRAR唯一的官网是这个 https://www.rarlab.com/ 其余的都不要相信. 现在的问题是:不要脸的中国代理强行捆绑广告:即使你花钱注册同样要面对弹窗广告!这就不可接受了! ...
- java学习笔记4——返回值
这个简单,返回值就是计算结果. 打个比方:个表格中我只要结果,不要经过,这个返回值就是结果.这个过程就是函数. 另外还有一个函数套用一个函数,被套用的函数的结果作为一个返回值给套用的外层函使用.比如: ...
- RxSwift學習教程之基礎篇
前言 我們在 iOS 開發過程中,幾乎無時無刻都要面對異步事件的處理.例如,按鍵點擊.數據保存..音頻後臺播放.交互動畫展示.這些事件並不具備特定時序性,甚至它們可能同時發生. 雖然 Apple 提供 ...
- MindManager 2019新版上市 ,了解一下!
所有的等待都是值得的!MindManager在蓄力一年后,给各位思维导图爱好者带来了全新的MindManager 2019 for Windows.全新的版本包含英语.德语.法语.俄语.中文.日语,新 ...
- 从无到有创建一个grunt项目
在安装好grunt的前提下创建一个grunt的项目: 1.首先创建一个项目文件 就叫grunt-project 2.进入这个文件 创建一个index.html 在创建一个js文件,进去创建一个inde ...
- 用shell编写一个三角形图案
第一种方法 #!/bin/bash read -p "请输入层数: " n for (( i=1; i<=$n;i++ ))do for (( j=n; j>=i; ...
- python二级登陆菜单
""" 1.三级菜单 注册 登陆 注销 2.进入每一个一级菜单,都会有下一级的菜单"""user_item = dict()try: whi ...
- Performance Co-Pilot
Install Performance Co-Pilot 提前安装依赖 [root@iZrj97j6t7ih9hgz1me35hZ ~]# cat install.sh yum install -y ...
- Css进阶练习(实现抽屉网样式布局)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...