蜗牛慢慢爬 LeetCode 2. Add Two Numbers [Difficulty: Medium]
题目
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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
翻译
给定两个非空的链表,表示两个非负整数。数字倒序存储,每个节点包含一个数字。将两个数字求和并将结果以链表形式返回
你可以假定数字不包含前导0(除了0本身之外)
输入:(2 - > 4 - > 3)+(5 - > 6 - > 4)
输出:7 - > 0 - > 8
Hints
Related Topics: Linked List, Math
注意简化代码
代码
Java
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode c1 = l1;
ListNode c2 = l2;
ListNode s = new ListNode(0);
ListNode d = s;
int sum = 0;
while(c1!=null||c2!=null){
sum /= 10;
if(c1!=null){
sum += c1.val;
c1 = c1.next;
}
if(c2!=null){
sum += c2.val;
c2 = c2.next;
}
d.next = new ListNode(sum%10);
d = d.next;
}
if(sum/10==1)
d.next = new ListNode(1);
return s.next;
}
}
Python
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
out = 0
result = ListNode(0)
l = result
while l1!=None or l2!=None:
x1 = l1.val if l1!=None else 0
x2 = l2.val if l2!=None else 0
l.next = ListNode((x1+x2+out)%10)
out = (x1+x2+out)/10
l = l.next
l1 = l1.next if l1!=None else l1
l2 = l2.next if l2!=None else l2
if out!=0:
l.next = ListNode(out)
return result.next
//better solution from discuss
class Solution(object):
def addTwoNumbers(self, l1, l2):
carry = 0
root = n = ListNode(0)
while l1 or l2 or carry:
v1 = v2 = 0
if l1:
v1 = l1.val
l1 = l1.next
if l2:
v2 = l2.val
l2 = l2.next
carry, val = divmod(v1+v2+carry, 10)
n.next = ListNode(val)
n = n.next
return root.next
蜗牛慢慢爬 LeetCode 2. Add Two Numbers [Difficulty: Medium]的更多相关文章
- 蜗牛慢慢爬 LeetCode 5.Longest Palindromic Substring [Difficulty: Medium]
题目 Given a string s, find the longest palindromic substring in s. You may assume that the maximum le ...
- 蜗牛慢慢爬 LeetCode 10. Regular Expression Matching [Difficulty: Hard]
题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single charac ...
- 蜗牛慢慢爬 LeetCode 1.Two Sum [Difficulty: Easy]
题目 Given an array of integers, return indices of the two numbers such that they add up to a specific ...
- 蜗牛慢慢爬 LeetCode 23. Merge k Sorted Lists [Difficulty: Hard]
题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- 蜗牛慢慢爬 LeetCode 15. 3Sum [Difficulty: Medium]
题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...
- 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]
题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]
题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]
题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
随机推荐
- TCP/IP协议的数据传输过程详解——IP与以太网的包收发操作
MTU:一个网络包的最大长度,以太网中一般是1500字节:(含有头部长度,包括IP头部,TCP头部,不包括MAC头部) MSS:除去头部后,一个网络包所能容纳的TCP的数据的最大长度 下图为TCP/I ...
- Java学习笔记三十一:Java 包(package)
Java 包(package) 一:包的作用: 如果我们在使用eclipse等工具创建Java工程的时候,经常会创建包,那么,这个包是什么呢. 为了更好地组织类,Java 提供了包机制,用于区别类名的 ...
- leetcode记录-回文数
判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向 ...
- gulp 输出到同一目录
gulp.task('jsx', function () { var src='app/script/**/*.jsx'; // src='app/script/components/selloff/ ...
- 20155318 《Java程序设计》实验五 (网络编程与安全)实验报告
20155318 <Java程序设计>实验五 (网络编程与安全)实验报告 实验内容 了解计算机网络基础 掌握Java Socket编程 理解混合密码系统 掌握Java 密码技术相关API的 ...
- SupperSocket深入浅出
这篇文章出要是SuperSocket底层如何接收数据 Process(ArraySegment<byte> segment) 获取加载数据(直到数据全部接收后返回) namespace S ...
- 【BZOJ4543】Hotel加强版
[BZOJ4543]Hotel加强版 题面 bzoj 洛谷 $ps:$在洛谷看题在bzoj交... 题解 我们分析一下这个问题,要怎么样的点才满足三点距离两两相等呢? 1.存在三个点有共同的$LCA$ ...
- Yii 2.0 使用验证码
Yii2.0 提供了验证码组件.调用起来比较方便.以登录页面添加验证码为例. 1. 模型中添加字段和验证规则. common\models\LoginForm 添加如下代码 public $captc ...
- Spring学习(三)-----Spring自动装配Beans
在Spring框架,可以用 auto-wiring 功能会自动装配Bean.要启用它,只需要在 <bean>定义“autowire”属性. <bean id="custom ...
- python全栈开发-面向对象-进阶
python_day_18 1,面向对象的三大特性是什么?继承,多态,封装2,什么是面向对象的新式类?什么是经典类?凡是继承object类都是新式类.凡是不继承object类都是经典类.3,面向对象为 ...