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 ...
随机推荐
- 1.Java
面向对象:封装,继承,多态 A组合B:那么B就可以调用A中的方法 A关联B:A中的成员变量是用B声明的 A和B的依赖关系:A中某个方法的参数是B声明的对象或者返回值类型是B的数据类型 Static方法 ...
- java锁
---恢复内容开始--- synchronized 互斥锁 synchronized(this) 当前类的所有synchronized(this) 都被锁了,还有synchronized static ...
- LDAP目录服务
LDAP目录服务 1.ldap目录服务介绍: 目录是一类为了浏览和搜索数据而设计的特殊的数据库,目录服务是按树状形式存储信息的,目录包含基于属性的描述信息,并且支持高级的过滤功能,一般来说,目录不支持 ...
- 分布式系统session同步解决方案
来源:架构师之路 session的概念 什么是session? 当浏览器端第一次访问web server时,server端会调用getSession()方法创建session对象,经过特殊算法计算出s ...
- QT中QMainWindow、QWidget、QDialog
QT中QMainWindow.QWidget.QDialog 简述 在分享所有基础知识之前,很有必要在这里介绍下常用的窗口-QWidget.QDialog.QMainWindow. 熟悉Qt的同学都应 ...
- QVector也是隐式数据共享的
Behind the scenes, QString uses implicit sharing (copy-on-write) to reduce memory usage and to avoid ...
- 70.纯 CSS 创作一只徘徊的果冻怪兽
原文地址:https://segmentfault.com/a/1190000015484852 感想:monster中边框角.上下动画.旋转动画.左右动画,眼睛中transform:scaleY(n ...
- Rabbitmq 安装后采坑
一.接手项目 接手项目后,按别人说的先安装什么,后安装什么然后就可以用了,也不去看什么.先开始安装的是otp_win64_19.1工具包和rabbitmq-server-3.6.5服务端,在win10 ...
- 《2018面向对象程序设计(Java)课程学习进度条》
周次 (阅读/编写)代码行数 发布博客量/博客评论数量 课堂/课余学习时间(小时) 最满意的编程任务 第一周 50/40 1/0 6/4 九九乘法表 第二周 100/80 1/0 6/8 实验5,6, ...
- JSFL元件类型判断 转载于 https://blog.csdn.net/linking530/article/details/8364600
//获取舞台上第一层第一帧上的全部元件 var els = fl.getDocumentDOM().getTimeline().layers[0].frames[0].elements; //遍历元件 ...