leetcode1:两数之和
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。
你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。
实例:
给定 nums = [2, 7, 11, 15],target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1]
1.我的思路:
可以提交但时间复杂度为O(n^2)。其中13到20行为了解决数组里有重复元素时,不能用index的情况。如图

class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
L=sorted(nums)
n = len(L)
i, j = 0, n-1 #首尾各一个指针
while i<j: #两指针的元素相加,等于target时,输出索引
if L[i] + L[j] == target: #在([3,3],9)测试为了避免index方法输出的索引
for value in range(n):
if nums[value] == L[i]:
m = value
break
for value in reversed(range(n)):
if nums[value] == L[j]:
n = value
break
return [m,n]
elif L[i] + L[j] < target:
i = i+1
elif L[i] + L[j] > target:
j = j-1
return 'no answer'
solution = Solution()
print(solution.twoSum([2,7,11,15],9))
2.参考了答案后的思路:
有重复元素时无法输出正确结果。

class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
d = {}
n = len(nums)
for value in nums:
if (target-value) not in d:
d[value] = nums.index(value)
elif (target-value) in d:
return [nums.index(target-value),nums.index(value)]
solution = Solution()
print(solution.twoSum([2,7,11,15],9))
leetcode1:两数之和的更多相关文章
- Leetcode1——两数之和 详细解析
Leetcode1--两数之和 题目分类:数组.哈希表的应用 1. 题目描述 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数 ...
- LeetCode1——两数之和
最近在家拧三阶魔方,把初级的玩法掌握了,也就是可以还原六个面了,速度不快,但是也很兴奋.三阶魔方的初级玩法按照套路拧就可以了,每一步需要完成的任务,该步骤转动的方法基本都是固定的,而且变化也并不是特别 ...
- Leetcode-1.两数之和
题目描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数 ...
- [Swift]LeetCode1 .两数之和 | Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- LeetCode1.两数之和 JavaScript
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 实例: 给定 nums = [2, 7, 11, 15], target ...
- Leetcode1.两数之和——简洁易懂
> 简洁易懂讲清原理,讲不清你来打我~ 输入一个数组和一个整数,从数组中找到两个元素和为这个整数,输出下标$
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- LeetCode 371. Sum of Two Integers (两数之和)
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- LeetCode 167. Two Sum II - Input array is sorted (两数之和之二 - 输入的是有序数组)
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
随机推荐
- (error) LOADING Redis is loading the dataset in memory
redis读取/重启之后读取报错:(error) LOADING Redis is loading the dataset in memory 1.redis将之持久化的数据重新写入,等待数据写入完成 ...
- Linux 修改zabbix server的web访问端口
在安装zabbix server的时候默认就安装了apache,zabbix依靠apache提供的web服务,修改Zabbix的浏览器访问端口,就是修改apache的服务端口(默认端口:80) 1.编 ...
- Delphi中Inputbox 和Inputquery 函数的使用
原文转自:http://blog.csdn.net/zengcong2013/article/details/18355959 inputbox的返回值是,你在输入框中输入的文字.而inputquer ...
- redis(1)--redis3.2.8安装
一.下载安装包 http://download.redis.io/releases/ 选择对应的版本 二.copy到对应的虚拟机上 运行 tar -zxvf redis-3.2.8 进行解压 更改 ...
- Android APK反编译(二)
参考:APK反编译 工具介绍 apktool 作用:资源文件获取,可以提取出图片文件和布局文件进行使用查看 dex2jar 作用:将apk反编译成java源码(classes.dex转化成jar文件) ...
- [C++]数组指针,数组引用,函数指针
数组指针是指一个指向数组的指针,例如有一个数组指针p指向一个数组a[],则 *p是取到这个数组,也就是说 *p=a,因此 **p =a[0], 它的定义为: ]; ]=&a; (*c)表示它是 ...
- 在Centos7上安装wxPython4.0.4
在linux上安装wxPython4.0.4时需要gtk+2.0,在安装wxPython4.0.4遇到以下错误. linux上是用pip安装wxPython4.0.4的,执行命令如下: pip ins ...
- react项目如何修改默认3000端口号
在运行react项目时,经常会遇到默认的3000端口被占用的情况,此时不想查找哪个程序占用了3000端口,想使用其他端口继续运行. 打开项目中的node_modules文件夹,找到react_scri ...
- 《Dare To Dream 》第三次作业--团队项目的原型设计与开发
一.实验目的与要求 1.掌握软件原型开发技术: 2.学习使用软件原型开发工具: 二.实验内容与步骤 任务1:针对实验六团队项目选题,采用适当的原型开发工具设计团队项目原型: 任务2:在团队博客发布博 ...
- java方法体
执行顺序: 静态代码块 mian方法 构造代码块 构造方法 静态代码块只执行一次.