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) ...
随机推荐
- node.js和JavaScript的关系
node.js是一个基于 Chrome V8 引擎的 JavaScript 运行时环境. 一.类比JavaScript和java JavaScript java V8 JVM node.js JRE ...
- 使用catsup快速建立个人博客
一.安装 time: 2016-01-2 20:30 1.使用pip安装catsup:(sudo) pip install catsup 从旧版本升级到新版本:(sudo) pip install c ...
- 记ASP.NET 使用 X509Certificate2 出现的一系列问题
在做微信支付退款的时候,由于需要使用到p12证书,结果就遇到一系列的坑.这里做个记录方便以后查阅. 原先加载证书的代码: X509Certificate2 cert = new X509Certifi ...
- opencv3.2.0形态学滤波之腐蚀
/* 腐蚀(erode)含义: 腐蚀和膨胀是相反的一对操作,所以腐蚀就是求局部最小值的操作,腐蚀操作使原图中 国的高亮部分被腐蚀,效果图比原图有更小的高亮的区域. 腐蚀函数原型API及参数同膨胀相同 ...
- 线程间的通信方式2--管道流Pipes
“管道”是java.io包的一部分.它是Java的特性,而不是Android特有的.一条“管道”为两个线程建立一个单向的通道.生产者负责写数据,消费者负责读取数据. 下面是一个使用管道流进行通信的例子 ...
- Android自定义View探索—生命周期
Activity代码: public class FiveActivity extends AppCompatActivity { private MyView myView; @Override p ...
- java基础(四) java运算顺序的深入解析
1. 从左往右的计算顺序 与C/C++不同的是,在Java中,表达式的计算与结果是确定的,不受硬件与环境的影响.如: int i = 5; int j = (i++) + (i++) +(i++) ...
- 的确,Java存在缺陷。但是……
[编者按]本文作者为资深码农 Tim Spann,主要讲述 Java 让人无法抗拒的众多优点以及一些些缺陷.本文系国内 ITOM 管理平台 OneAPM 编译呈现,以下为正文. 早在90年代中期,笔者 ...
- leetcode之二叉树的层序遍历
1.题目描述 2.题目分析 二叉树的层序遍历主要算法思想是使用 队列这一数据结构实现,这个数据结构多应用在和 图相关的算法.例如图的广度优先遍历就可以使用队列的方法实现.本题的关键在于如何识别出一层已 ...
- 记录今天客户的SQLSERVER启动不起来( 错误9003)的解决过程2013-11-26
记录今天客户的SQLSERVER启动不起来( 错误9003)的解决过程2013-11-26 今天一大早上班就接到客户的电话,说:SQLSERVER启动不起来,业务系统使用不了 于是我就使用QQ远程,帮 ...