LeetCode 2. Add Two Numbers 解题报告
题意:
思路:
C++ Code
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode preHead(), *p = &preHead;
int extra = ;
while (l1 || l2 || extra) {
int sum = (l1 ? l1->val : ) + (l2 ? l2->val : ) + extra;
extra = sum / ;
p->next = new ListNode(sum % );
p = p->next;
l1 = l1 ? l1->next : l1;
l2 = l2 ? l2->next : l2;
}
return preHead.next;
}
};
Python Code
# 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
"""
p = preHead = ListNode(0)
extra = 0
while l1 or l2 or extra:
extra, val = divmod((l1 and l1.val or 0) + (l2 and l2.val or 0) + extra, 10)
p.next = ListNode(val)
p = p.next
if l1:
l1 = l1.next
if l2:
l2 = l2.next
return preHead.next
JS Code
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
var addTwoNumbers = function(l1, l2) {
var preHead = new ListNode(0)
var p = preHead
var extra = 0
while(l1 !== null || l2 !== null || extra !== 0) {
var sum = (l1 && l1.val || 0) + (l2 && l2.val || 0) + extra
extra = (sum > 9) ? 1 : 0
p.next = new ListNode(sum % 10)
p = p.next
l1 = l1 && l1.next || null
l2 = l2 && l2.next || null
}
return preHead.next
};
LeetCode 2. Add Two Numbers 解题报告的更多相关文章
- LeetCode 2 Add Two Sum 解题报告
LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...
- 【LeetCode】386. Lexicographical Numbers 解题报告(Python)
[LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- LeetCode: Add Two Numbers 解题报告
Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...
- LeetCode 728 Self Dividing Numbers 解题报告
题目要求 A self-dividing number is a number that is divisible by every digit it contains. For example, 1 ...
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 【LeetCode】165. Compare Version Numbers 解题报告(Python)
[LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】91. Decode Ways 解题报告(Python)
[LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
随机推荐
- TCP实现P2P通信
Internet的迅速发展以及IPv4 地址数量的限制使得网络地址翻译(NAT,Network Address Trans2lation)设备得到广泛应用.NAT设备允许处于同一NAT后的多台主机共享 ...
- --@angularjs--理解Angular中的$apply()以及$digest()
$apply() 和 $digest() 在 AngularJS 中是两个核心概念,但是有时候它们又让人困惑.而为了了解 AngularJS 的工作方式,首先需要了解 $apply() 和 $dige ...
- MFC中在基于对话框的窗体中使用CFileDialog导致菜单栏变灰的解决方案
CSDN的博客编辑器实在是难用……转战博客园 直接把CSDN发的搬过来了 ————————————————————————————我是分割线———————————————————————————— 第 ...
- jQuery wrap wrapAll wrapInner使用
jQuery wrap wrapAll wrapInner使用 <%@ page language="java" import="java.util.*" ...
- Spring MVC整合DWR
http://blog.csdn.net/geloin/article/details/7537148 基本上与上文描述的情况一致: 在Controller中可以进行Service层的调用: 如果需要 ...
- border-radius是向元素添加圆角边框的方法
border-radius:10px; /* 所有角都使用半径为10px的圆角 */ border-radius: 5px 4px 3px 2px; /* 四个半径值分别是左上角.右上角.右下角和左下 ...
- [Linux] 使用openssl实现RSA非对称加密
简单定义:公钥和私钥,加密和解密使用的是两个不同的密钥,所以是非对称 系统:ubuntu 14.04 软件:openssl java php 生成公钥私钥 使用命令生成私钥: openssl genr ...
- window下redis的安装
1.使用phpinfo()函数查看PHP的版本信息,这会决定扩展文件版本2.根据PHP版本号,编译器版本号和CPU架构,选择php_redis-2.2.5-5.5-ts-vc11-x86.zip和ph ...
- ECMAScript 6 笔记(五)
Iterator和for...of循环 1. Iterator(遍历器)的概念 Iterator接口的目的,就是为所有数据结构,提供了一种统一的访问机制,即for...of循环 遍历器(Iterato ...
- 基于python的互联网软件测试开发(自动化测试)-全集合
基于python的互联网软件测试开发(自动化测试)-全集合 1 关键字 为了便于搜索引擎收录本文,特别将本文的关键字给强调一下: python,互联网,自动化测试,测试开发,接口测试,服务测试,a ...