Leetcode 解题 Add Two Numbers Python
原题:
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
代码如下,递归调用add函数即可:
class Solution:
# @param {ListNode} l1
# @param{ListNode}l2
# return {ListNode} def addTwoNumbers(self, l1, l2):
return self.add(l1, l2)
def add(self, l1, l2, s=0):
l = ListNode(s)
if l1:
l.val += l1.val
l1 = l1.next
if l2:
l.val += l2.val
l2 = l2.next
s= l.val/10
l.val = l.val % 10
if l1 or l2 or s: l.next = self.add(l1, l2, s)
return l
Leetcode 解题 Add Two Numbers Python的更多相关文章
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- LeetCode:1. Add Two Numbers
题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...
- [Leetcode Week15] Add Two Numbers
Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...
- 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 2. add two numbers && 单链表
add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...
- [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 ...
随机推荐
- linux-多线程
一.什么是线程? 线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立执行的基本单位.线程自己基本上不拥有系统资源,仅仅拥有一点在执行中不可缺少的资源(如程序计数器,一组寄存器和 ...
- 【S13】vector和string优先于动态分配的内存
1.使用new动态分配内存,必须承担如下责任: a.使用delete释放内存: b.确保使用了正确的形式,delete与new的形式要匹配: c.不能重复delete. 2.使用vector和stri ...
- PHP工具下载地址
Zend Debugger下载地址:http://www.zend.com/en/products/studio/downloads 需要先注册一下,然后才能进行下载.
- mfc开发问题_v1
1. 设置对话框按钮背景图片? 首先,设置对话框按钮的属性为Bitmap,然后导入资源文件(一个你需要作为背景的小图片),最后在该对话框类的OnInitDialog函数中添加如下代码: //设置对话框 ...
- 理解URI和URL
1)定义: URI: Uniform Resource Identifier,通用资源标识符 ---是一个用于标识某一互联网资源名称的字符串(by 维基百科) URL:Uniform Resource ...
- WPF遇到无边框的问题
今天做一个项目采用的是WPF开发并且在制作窗体的时候用到无边框的问题,由于WPF开发和winform开发用点不一样, 遇到了这个问题就帮这个遇到问题的解决方法写下来方便以后忘记了和给一些遇到的朋友做一 ...
- Adapter 适配器模式
将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 目标接口(Target):客户所期待的接口.目标可以是具体的或抽象的类,也可 ...
- "make_path" is not exported by the File::Path modul
之前正常运行的perl脚本换了一个环境突然报 从原来的make_path 和 remove_tree改为现在的mkpath 和 rmtree就好了. File::Path version is 1.0 ...
- Asp.Net MVC--Controller激活2
在使用MVC项目中,如果激活控制器,则就会向前台返回action执行的结果. 很多时候,根据需求,手动激活控制器来向客户端返回结果. 一.激活实例代码1 这是在Global文件中使用 var rout ...
- java中关于时间的格式化
long time = System.currentTimeMillis(); SimpleDateFormat format = new SimpleDateFormat(); String s = ...