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.两数之和——简洁易懂
> 简洁易懂讲清原理,讲不清你来打我~ 输入一个数组和一个整数,从数组中找到两个元素和为这个整数,输出下标![在这里插入图片描述](https://img-blog.csdnimg.cn/img ...
- 给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X
题目:给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X 思路一: 1,先采用归并排序对这个数组排序, 2,然后寻找相邻<k,i>的两数之和sum,找到恰好sum>x的 ...
- LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$
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 ...
随机推荐
- 关于Python课程的一些思考。
出于对网络爬虫的好奇,我选修了Python程序设计,至于pyhton还能干啥还不太清除,只觉得爬一些数据很有意思,所以希望老师讲一些数据分析之类的技术.学完课程希望能分析一些数据,比如:还有: 上课的 ...
- majingwei 利用xml导出word文件---换行
xml不能识别<br>,需要将换行标记转换成<w:br/>
- Android 开发 获取设备信息与App信息
设备信息 设备ID(DeviceId) 获取办法 android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager ...
- 在c#中利用keep-alive处理socket网络异常断开的方法
本文摘自 http://www.z6688.com/info/57987-1.htm 最近我负责一个IM项目的开发,服务端和客户端采用TCP协议连接.服务端采用C#开发,客户端采用Delphi开发.在 ...
- kubenetes服务发现
一.基于 iptables 的 Service 实现 Pod的ip地址不是固定了.Service通过selector属性和后端Pod关联,被selector选中的Pod被称为Service的Endpo ...
- 使用shell命令给文件中每一行的前面、后面添加字符
shell command shell给一个文件中的每一行开头插入字符的方法:awk '{print "xxx"$0}' fileName shell给一个文件中的每一行结尾插入字 ...
- python类的动态属性设置
需求:根据入参,动态添加设置类的属性 实现: self.__setattr__ self.__getattribute__
- 深度学习实战-强化学习-九宫格 当前奖励值 = max(及时奖励 + 下一个位置的奖励值 * 奖励衰减)
强化学习使用的是bellmen方程,即当前奖励值 = max(当前位置的及时奖励 + discout_factor * 下一个方向的奖励值) discount_factor表示奖励的衰减因子 使用 ...
- socket.io诡异的问题
在socket.io客户端连接的时候,如果传入的query包含“sid”这个键时会报错,不知道具体原因.
- Zabbix客户端日志出现(Not all processes could be identified, 解决
场景:因为使用了netstat -p参数. 权限问题,zabbix_agentd是zabbix用户启动的,默认不能执行netstat -p等命令,导致从服务器取到的自动发现脚本为空 (Not all ...