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 之外,这两个数字都不会以零开头. 示例: 输入: ...
随机推荐
- 第6章 服务模式 Service Interface(服务接口)
Service Interface(服务接口) 上下文 您正在设计企业应用程序,并且需要能够通过网络使用其部分功能.此功能需要能够被各类系统使用,因此互操作性是设计的重要方面.除互操作性之外,可能还需 ...
- 无序列表属性 隐藏方式 JS简介
今天考试了,整理一下错题. 1.无序列表的属性 list-style 分为三小类 (1)list-style-type none:无标记. disc:实心圆(默认). circle:空心圆. squa ...
- 让break跳出外层循环的方法
demo //在里层循环里面,想办法让外层循环的条件不成立,就可以控制外层循环结束. for(var i = 0 ; i < 10; i++){ alert(i) for(var j = 0 ; ...
- Android一对多蓝牙连接示例APP
一对多蓝牙连接示例,基于Google BluetoothChat修改,实现一对多聊天(一个服务端.多个客户端),类似聊天室. 主要功能: 客户端的发出的消息所有终端都能收到(由服务端转发) 客户端之间 ...
- VS2015 & ReSharper CTRL
如下为个人比较常用的快捷键,部分快捷键有进行修改. 一.VS偏好设置: 1.转到定义:F12 2.查找所有引用:Shift + F12 3.跳转到指定的某一行:Ctrl + G (or 单击状态栏中的 ...
- CV与IP:基础,经典以及最近发展
原文链接:http://blog.csdn.net/dcraw/article/details/7617891 原文编写层层递进,逻辑清晰: 不过看这篇转载可以少点击鼠标:http://blog.cs ...
- preparedStatement平台:
public class cs{ public static void main(String[] args){ try{ class.forName("com.mysql.jdbc.Dri ...
- Mysql 5.7 for windows 免安装版(解压版)安装和配置
网上写的不近详细,这里重新整理下. 准备: 1.windows操作系统 2.mysql 的解压版压缩文件 第一步: 解压mysql的压缩包到你的安装目录,因为是虚拟机,这里我就安装在C盘下:C:\my ...
- C++ STL - queue使用详解
c++队列模板类的定义在<queue>头文件中,queue 模板类需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的,默认为deque 类型. 下面详细介绍 ...
- [tyvj-1194]划分大理石 二进制优化多重背包
突然发现这个自己还不会... 其实也不难,就和快速幂感觉很像,把物品数量二进制拆分一下,01背包即可 我是咸鱼 #include <cstdio> #include <cstring ...