two sum[easy]
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
if len(nums) <2:
if nums[0] != target:
return "no this"
return [0]
else:
nums_new = nums
for i in range(len(nums_new)):
if target-nums_new[i] in nums_new[i+1:]:
return [i,nums_new.index(target-nums_new[i],i+1)]
return False
result=Solution().twoSum([14,3,5,6,3],6)
print(result)
two sum[easy]的更多相关文章
- LeetCode--Array--Two sum (Easy)
1.Two sum (Easy)# Given an array of integers, return indices of the two numbers such that they add u ...
- 【leetcode】Minimum Path Sum(easy)
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- 【leetcode】Two Sum (easy)
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- [leetcode] #112 Path Sum (easy)
原题链接 题意: 给定一个值,求出从树顶到某个叶(没有子节点)有没有一条路径等于该值. 思路: DFS Runtime: 4 ms, faster than 100.00% of C++ class ...
- Leetcode——Two Sum(easy)
题目:Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1] 代码: ...
- Leetcode--1. Two Sum(easy)
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- Two Sum [easy] (Python)
由于题目说了有且只有唯一解,可以考虑两遍扫描求解:第一遍扫描原数组,将所有的数重新存放到一个dict中,该dict以原数组中的值为键,原数组中的下标为值:第二遍扫描原数组,对于每个数nums[i]查看 ...
- LeetCode_112. Path Sum
112. Path Sum Easy Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...
- 【目录】Leetcode
Leetcode 1.动态规划 Palindrome Partitioning II(hard) ☆ Distinct Subsequences(hard) Edit Distance (hard) ...
随机推荐
- IBM V7000错误代码及解决
https://www.ibm.com/support/knowledgecenter/zh/ST3FR7_7.7.1/com.ibm.storwize.v7000.771.doc/svc_error ...
- ubuntu下使用python3的有些库时,解决"raise ImportError(str(msg) + ', please install the python3-tk package') ImportError: No module named '_tkinter', please install the python3-tk package"的错误
问题: 在Ubuntu下使用matplotlib这个库时,运行时出现如下错误: raise ImportError(str(msg) + ', please install the python3-t ...
- 阿里react整合库dva demo分析 [转]
同,也是工作中需要,用到 dva , 也找了些文章参考知识点. 更多:http://www.cnblogs.com/heyuqing/p/6844098.html 以下内容为摘出 mark 接着踩 ...
- 激活函数(relu,prelu,elu,+BN)对比on cifar10
激活函数(relu,prelu,elu,+BN)对比on cifar10 可参考上一篇: 激活函数 ReLU.LReLU.PReLU.CReLU.ELU.SELU 的定义和区别 一.理论基础 ...
- Flutter TabBar
先看一下Tab的构造方法: TabBar({ Key key, @required this.tabs, this.controller, this.isScrollable: false, this ...
- Vue 框架-04-计算属性
Vue 框架-04-计算属性 计算属性是什么? 大家可以去看官网解释:计算属性和侦听器 今天的第一个小实例: 为啥先放折磨一个实例,之前数据绑定的就已经可以实现了,看起来那么简单,就是为了告诉大家,当 ...
- java 接口默认修饰符
概论: java接口可以是public 的,也可以是friendly的,但一定是abstracted的. java接口里的方法只能是public的.abstract的. java接口里的成员变量只能是 ...
- VIM 乱码终极解决
原文链接:http://blog.163.com/mageng11@126/blog/static/1408083742012128105645169/ 关于vim乱码,这篇文章讲的很详细,mark一 ...
- java基础(十) 数组类型
1. 数组类简介 在java中,数组也是一种引用类型,即是一种类. 我们来看一个例子,理解一下数组类: public static void main(String[] args) { Class ...
- Office 365实现单点登录系列(5)—配置单点登录
这是单点登录系列的最后一篇文章,前面4篇文章其实都是在为这篇文章的内容做准备,我把这四篇文章的链接放在下面,如果大家有需要,可以参考我以下的链接: Office 365实现单点登录系列(1)—域环境搭 ...