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) ...
随机推荐
- Oracle扩容日志文件
0.检查当前数据库日志切换频率 select * from v$log_history where first_time>=to_date('2017-10-18','yyyy-mm-dd') ...
- PHP中的prepare准备语句的意义
mysqli和PDO扩展都有prepare这个语法,刚开始以为只是单纯的语法,没想到,还是有实际意义的: “每次发送查询语句给MySQL服务时,都必须解析该查询的语法,确保结构正确并能够执行.这是这个 ...
- ci 3.0 默认路由放在子文件夹 无法访问的解决办法
比方说你想配置默认路由为: $route['default_controller'] = 'index/home'; ci3.0之前是可以放在 controllers中的子文件夹中的,但是到了ci ...
- javaSE——字符流
字符流: 读取数据的单位是字符,即每次可以读取至少一个字符(一个字母.数字.汉字.符号). 和字节流一样,管子搭载的对象不同,则字符流就不同. 类 FileReader: 用于读取文件的便捷类. 继承 ...
- 使用IntelliJ IDEA配置Erlang开发环境
这篇文章比较详细,感谢作者,拷贝过来做个记录 ————————————————————————————————————————————————————————————————————————————— ...
- CVE-2017-11882钓鱼样本构造
前言 漏洞详情: https://embedi.com/blog/skeleton-closet-ms-office-vulnerability-you-didnt-know-about 最近的一个影 ...
- 课后作业week 5 —— 两款修图软件优势及创新分析
由于我平时没事也会修修照片什么的,也用过一些不同种类的修图软件,这次作业就选择了其中两款比较热门的软件进行分析. 说到手机修图app,很多人很容易想到“美图秀秀”,的确这款app在修图软件领域的确算的 ...
- Oracle 查看锁情况
/*查看锁(lock)情况*/ SELECT ls.osuser os_user_name, ls.username user_name, decode(ls.type, 'RW', 'Row wai ...
- 【Kettle】8、变量参数传递介绍
本文为转载,感觉作者的辛勤劳作:http://blog.csdn.net/rotkang/article/details/21008271 ------------------------------ ...
- [SQLServer] 数据库SA用户被锁定或者忘记密码的恢复
一.以管理员权限运行命令提示符 CMD C:\>net stop mssqlserver您想继续此操作吗? (Y/N) [N]: y C:\>net start mssqlserver / ...