Two Sum & Add Two Numbers
Two Sum
题目:https://leetcode.com/problems/two-sum/
class Solution(object):
def twoSum(self, nums, target):
map = {}
for index, value in enumerate(nums):
if target - value in map:
return [map[target - value] + 1, index + 1]
map[value] = index
Add Two Numbers
题目:https://leetcode.com/problems/add-two-numbers/
# 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
"""
flag = 0
head = ListNode(0)
pos = head
while l1 is not None and l2 is not None:
l3 = ListNode(l1.val + l2.val + flag)
flag = 0
if l3.val >= 10:
l3.val -= 10
flag = 1
l1 = l1.next
l2 = l2.next
pos.next = l3
pos = pos.next
while l1 is not None:
l3 = ListNode(l1.val + flag)
flag = 0
if l3.val >= 10:
l3.val -= 10
flag = 1
l1 = l1.next
pos.next = l3
pos = pos.next
while l2 is not None:
l3 = ListNode(l2.val + flag)
flag = 0
if l3.val >= 10:
l3.val -= 10
flag = 1
l2 = l2.next
pos.next = l3
pos = pos.next
if flag == 1:
l3 = ListNode(1)
pos.next = l3
return head.next
Two Sum & Add Two Numbers的更多相关文章
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 52. 不用+、-、×、÷做加法[add two numbers without arithmetic]
[本文链接] http://www.cnblogs.com/hellogiser/p/add-two-numbers-without-arithmetic.html [题目] 写一个函数,求两个整数的 ...
- Leetcode-2 Add Two Numbers
#2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits ...
- [CareerCup] 2.5 Add Two Numbers 两个数字相加
2.5 You have two numbers represented by a linked list, where each node contains a single digit. The ...
- [LintCode] Add Two Numbers 两个数字相加
You have two numbers represented by a linked list, where each node contains a single digit. The digi ...
- leetcode2:Add Two Numbers
Add Two Numbers Total Accepted: 55216 Total Submissions: 249950My Submissions You are given two link ...
- leetcode 第二题Add Two Numbers java
链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
随机推荐
- 07 Linux su和sudo命令的区别
一. 使用 su 命令临时切换用户身份 1.su 的适用条件和威力 su命令就是切换用户的工具,怎么理解呢?比如我们以普通用户beinan登录的,但要添加用户任务,执行useradd ,beinan用 ...
- shiro 标签
在使用Shiro标签库前,首先需要在JSP引入shiro标签: <%@ taglib prefix="shiro" uri="http://shiro.apache ...
- [HTML] CSS3 文本效果
CSS3 文本效果 CSS3中包含几个新的文本特征. 在本章中您将了解以下文本属性: text-shadow word-wrap 浏览器支持
- iOS开发~CocoaPods使用详细说明【转】
一.概要 iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来方便的统一管理这些第三方库. 二.安装 由于 ...
- PHP时间日期
PHP常用的几个时间日期函数有:date(),mktime(),strtotime(); 一.string date ( string $format [, int $timestamp ] ) 函数 ...
- 移动端自动化环境搭建-JDK的安装
一.安装jdk A.安装依赖 JDK作为JAVA开发的环境,不管是做JAVA开发的学生,还是做安卓开发的同学,都必须在电脑上安装JDK. B.安装过程 安装JDK 选择安装目录 安装过程中会出现两次 ...
- ubuntu 开机显示错误:无法应用原保存的显示器配置
无法应用原保存的显示器配置CRTC 63:尝试 800x600@60Hz 模式输出在 1366x768@60Hz (通过 0)CRTC 63:尝试 2560x1600@60Hz 模式输出在 1366x ...
- CSS+HTML网页设计与布局(学习笔记1)
1.在宽度未知时,使div块居中,可以添加以下属性: display:table;margin:0 auto;
- <COM原理和应用>第七章的ITextObject代码是什么?
第7章中有如下的描述:-----------------------------------为了在程序中使用"Text.Object"文本对象,我们利用ClassWizard引 ...
- arpg网页游戏之地图(四)
这一节主要是针对上一节的补充,关于地图的优化策略上一节中已经涉及了一些,这一节具体说下. 地图块加载队列:就拿1280*800的屏幕分辨率来讲,大约需要加载的地图块为30~35块之间,如果这个时候一下 ...