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 ...
随机推荐
- 【1】第一次电话面试---上海EMC
时间是2016//11月,投的是上海的EMC2公司的JavaWeb开发岗,第一次接到的电话面试,问的题目很基础基础,很遗憾,本人在掌握的太不好,回答的很乱,目测定挂.下面记下HR问的问题及回答. 首先 ...
- visual.studio.15.preview5 编译器
前段时间微软更新了新版开发工具visual studio 15 preview5,安装后连文件结构目录都变了,想提取编译器还找不到. 不是原来的VC\BIN目录,已迁移到IDE\MSVC\14.10. ...
- VS2010快捷键大全
VS2010版快捷键 Ctrl+E,D ----格式化全部代码 Ctrl+E,F ----格式化选中的代码 CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CTRL + O ...
- 两个二进制数多少个位(bit)不同
class Solution { public: /** * 获得两个整形二进制表达位数不同的数量 * * @param m 整数m * @param n 整数n * @return 整型 */ in ...
- tracer
http://people.canonical.com/~acelan/coscup-2010/Debugging%20Linux%20Kernel%20by%20Ftrace.pdf http:// ...
- C++设计模式-Factory工厂模式
Factory1.定义创建对象的接口,封装对象的创建2.将实际创建工作延迟到子类中,例如,类A中药使用类B,B是抽象父类,但是在类A中不知道具体要实例化哪一个B的子类,但是在类A的子类D中是可以知道的 ...
- 在win server 2003上安装SQL Server 2008的步骤
1.安装Microsoft .NET Framework 3.5 Service Pack 1,下载地址:http://www.microsoft.com/zh-cn/download/confirm ...
- JS-改变页面的颜色(三)
需求:点击页面的按钮,改变页面的颜色 思路:一先画出最简单的页面,二想办法获取页面的body节点,三想办法修改body节点的背景颜色属性,四通过一个方法获取随机的颜色值 和第二个例 ...
- 使用JDBC进行批处理
在实际的项目开发中,有时候需要向数据库发送一批SQL语句执行,这时应避免向数据库一条条的发送执行,而应采用JDBC的批处理机制,以提升执行效率. JDBC实现批处理有两种方式:statement和pr ...
- javascript立即执行函数 (function(){})()
看到一段代码: (function(){ var outer = $('#subject'); outer.find('li').on('mouseover', mouseover); })() ( ...