leetcode 【 Two Sum 】python 实现
题目:
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
代码:oj测试通过 Runtime: 54 ms
class Solution:
# @return a tuple, (index1, index2)
def twoSum(self, num, target):
number_index={}
for i in range(len(num)):
if number_index.has_key(target-num[i]):
return (number_index[target-num[i]]+1,i+1)
number_index[num[i]]=i
思路:
这个不是我想的。网上一大堆,不多说了。
leetcode 【 Two Sum 】python 实现的更多相关文章
- [leetcode]Path Sum @ Python
原题地址:https://oj.leetcode.com/problems/path-sum/ 题意: Given a binary tree and a sum, determine if the ...
- leetcode Combination Sum python
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- leetcode two sum python
class Solution: # @param {integer[]} nums # @param {integer} target # @return {integer[]} def twoSum ...
- LeetCode:Path Sum I II
LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...
- 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)
剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...
- [LeetCode]题解(python):113 Path Sum II
题目来源 https://leetcode.com/problems/path-sum-ii/ Given a binary tree and a sum, find all root-to-leaf ...
- [LeetCode]题解(python):112 Path Sum
题目来源 https://leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if the tree ha ...
- [LeetCode]题解(python):064-Minimum Path Sum
题目来源 https://leetcode.com/problems/minimum-path-sum/ Given a m x n grid filled with non-negative num ...
- [LeetCode]题解(python):124-Binary Tree Maximum Path Sum
题目来源: https://leetcode.com/problems/binary-tree-maximum-path-sum/ 题意分析: 给定一棵树,找出一个数值最大的路径,起点可以是任意节点或 ...
- [LeetCode]题解(python):040-Combination Sum II
题目来源 https://leetcode.com/problems/combination-sum-ii/ Given a collection of candidate numbers (C) a ...
随机推荐
- JAXB介绍一
参考博客: https://www.cnblogs.com/chenbenbuyi/p/8283657.html https://www.cnblogs.com/cnsdhzzl/p/8390514. ...
- Android studio 配置忽略
直接在Ignored Files选项里点击+号,在弹出的对话框选择第二项,然后依次输入上面包含的 .gradle .idea build 三个文件夹目录,再选择第一项,找到local.properti ...
- iOS 点击左上角系统返回按钮方法
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endBackground) name:UIAppl ...
- SqlServer Alwayson 搭建排错记录(一)
这几天搭建alwayson,碰到一堆问题,解决起来花了不少时间,特此记下几个有代表性的,以免以后再碰到做重复功. 一.创建可用性组 使用SSMS的创建可用性组向导,添加了一个主副本node1,一个辅助 ...
- Java 集合框架_上
集合框架被设计成要满足以下几个目标. 该框架必须是高性能的.基本集合(动态数组,链表,树,哈希表)的实现也必须是高效的. 该框架允许不同类型的集合,以类似的方式工作,具有高度的互操作性. 对一个集合的 ...
- 【转】iOS开发4:关闭键盘
在 iOS 程序中当想要在文本框中输入数据,轻触文本框会打开键盘.对于 iPad 程序,其键盘有一个按钮可以用来关闭键盘,但是 iPhone 程序中的键盘却没有这样的按钮,不过我们可以采取一些方法关闭 ...
- Java反射得到属性的值和设置属性的值
package com.whbs.bean; public class UserBean { private Integer id; private int age; private String n ...
- ZJOI2004 午餐
题目传送门 嗯--我承认我看了题解,不过好歹有了点自己的思路,大约蒙出来了\(30\%\)(个人感觉)-- 学会\(DP\),任重而道远啊! Step1.贪心排序 先将每个人按吃饭的快慢排序,然后再进 ...
- java Html&JavaScript面试题:用table显示n条记录,每3行换一次颜色,即1,2,3用红色字体,4,5,6用绿色字体,7,8,9用红颜色字体。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- axios常见传参方式
1:get请求 一般发送请求是这么写 axios.get('/user?id=12345&name=user') .then(function (res) { console.log(res) ...