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) ...
随机推荐
- MVC 导出Execl 的总结几种方式 (三)
第三种方式呢,就是借用第三方插件 NPOI 来实现Execl 导出 第一步:在NuGut包中下载NPOI 组件 第二步:编辑控制器代码 public FileResult ExcelNewKeyPro ...
- autofac初识
在开始autofac时,有必要先了解两个关键词:“控制反转(IoC/Inverse Of Control)”与“依赖注入(DI/Dependence injection)”. 控制反转(IoC):它把 ...
- JSONP 教程
JSONP 教程 本章节我们将向大家介绍 JSONP 的知识. Jsonp(JSON with Padding) 是 json 的一种"使用模式",可以让网页从别的域名(网站)那获 ...
- 转 mysqli 事务常用方法
原文:mysqli 事务常用方法 1. //打开(true)或关闭(false)本次数据库连接的自动命令提交事务模式 //参数如果设置为 FALSE,则表示关闭 auto-commit.如果设置为 T ...
- mysqli返回受影响行数
参考链接:http://php.net/manual/en/mysqli.affected-rows.php /* update rows */ $mysqli->query("UPD ...
- opencv3.2.0形态学滤波之腐蚀
/* 腐蚀(erode)含义: 腐蚀和膨胀是相反的一对操作,所以腐蚀就是求局部最小值的操作,腐蚀操作使原图中 国的高亮部分被腐蚀,效果图比原图有更小的高亮的区域. 腐蚀函数原型API及参数同膨胀相同 ...
- 大数据平台的技术演化之路 诸葛io平台设计实例
如今,数据分析能力正逐渐成为企业发展的标配,企业通过数据分析的过程将数据中的信息提取出来,进行处理.识别.加工.呈现,最后成为指导企业业务发展的知识和智慧.而处理.识别.加工.呈现的过程从本质上来讲, ...
- ArcEngine对Blob字段赋值的方法
今天在测试数据入库程序,发现对某个图层操作之后,调用StopOperation,会出现“尝试写入或读取受保护的内存”错误. 经过测试,最终发现是因为该图层包含有Blob字段,而代码没有专门对Blob字 ...
- Qt——元对象和属性机制
http://www.cnblogs.com/hellovenus/p/5582521.html 一.元对象 元对象(meta object)意思是描述另一个对象结构的对象,比如获得一个对象有多少成员 ...
- CCSUOJ评测系统——第四次scrum冲刺
1.小组成员 舒 溢 许嘉荣 唐 浩 黄欣欣 廖帅元 刘洋江 薛思汝 2.最终成果及其代码仓库链接 CCSU评测系统 代码仓库 3.评测系统功能 用户注册 用户可选题目进行提交 用户做题结果 排名功能 ...