【leetcode】1. Two Sums
题目
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
思路
首先考虑到时间复杂度O(N^2)的循环遍历,leetcode不能忍。
第二种方案复杂度为O(N),遍历数组时,以数值为key,索引为value建立字典,然后每次target值减当前值从字典中获取index,index小于i则说明在字典中存在。
代码O(N^2)
class Solution1:
# @param {integer[]} nums
# @param {integer} target
# @return {integer[]}
def twoSum(self, nums, target):
rlt = []
if not nums:
return rlt
for i in range(len(nums)):
for j in range(len(nums[i+1:])):
if nums[i] + nums[i+j] == target:
rlt.append(i)
rlt.append(i+j)
return rlt
代码O(N)
class Solution:
# @param {integer[]} nums
# @param {integer} target
# @return {integer[]}
def twoSum(self, nums, target):
rlt = []
if not nums:
return rlt
num_dict = {}
for i in range(len(nums)):
if nums[i] not in num_dict:
num_dict[nums[i]] = i
print nums[i],i
j = num_dict.get(target-nums[i], -1)
if j < i and j > -1:
rlt.append(j+1)
rlt.append(i+1)
return rlt
return rlt
【leetcode】1. Two Sums的更多相关文章
- 【LeetCode】974. Subarray Sums Divisible by K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 前缀和求余 日期 题目地址:https:/ ...
- 【leetcode】974. Subarray Sums Divisible by K
题目如下: Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have ...
- 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)
[LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
随机推荐
- 搭建ftp环境
首先明确,ftp站点设置在服务器上,而在客户端上来使用ftp工具来进行上传文件 具体环境搭建如下两个链接,一个server2003,一个是win7 server2003:http://jingyan. ...
- Rotate List —— LeetCode
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- mac下的改装人生——第一次拆卸mbp,加入内存
这是我第一次拆我的mbp哈,心情无比激动. 在家门口电脑维修店的老板那里借来了螺丝刀.回家以后立刻开始了改装行动 我的MC700后盖是可以全部打开的,总共大概是10个螺丝的样子,上面靠右边的三颗是长螺 ...
- python 发送邮件例子
想到用python发送邮件 主要是服务器 有时候会产生coredump文件 ,然后因为脚本重启原因,服务器coredump产生后会重启 但是没有主动通知开发人员 想了下可以写个脚本一旦产生cored ...
- shell 二元操作符 =~
17:14 [logc@a005.client.hadoop.qingdao.youku]$ a=1 17:14 [logc@a005.client.hadoop.qingdao.youku]$ if ...
- Servlet中获取JSP内置对象
方便自己查询,嫌低级的勿喷.... 1.request 在servlet的doGet和doPost的参数中就有HttpServletRequest req参数,而JSP内置request对象就是Htt ...
- "org.eclipse.wst.validation" has been removed
导出maven工程遇到的问题,"org.eclipse.wst.validation" has been removed,还以为是工程本身的问题,后来发现是eclipse的问题. ...
- Hadoop HDFS (3) JAVA訪问HDFS
如今我们来深入了解一下Hadoop的FileSystem类. 这个类是用来跟Hadoop的文件系统进行交互的.尽管我们这里主要是针对HDFS.可是我们还是应该让我们的代码仅仅使用抽象类FileSyst ...
- rsyslogd配置文件详解
非常详细的rsyslogd配置文件解析 rsyslog服务和logrotate服务=========================================================== ...
- python字符串replace()方法
python字符串replace()方法 >>> help(str.replace)Help on method_descriptor:replace(...) S.repla ...