给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。

你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。

实例:

给定 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:两数之和的更多相关文章

  1. Leetcode1——两数之和 详细解析

    Leetcode1--两数之和 题目分类:数组.哈希表的应用 1. 题目描述 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数 ...

  2. LeetCode1——两数之和

    最近在家拧三阶魔方,把初级的玩法掌握了,也就是可以还原六个面了,速度不快,但是也很兴奋.三阶魔方的初级玩法按照套路拧就可以了,每一步需要完成的任务,该步骤转动的方法基本都是固定的,而且变化也并不是特别 ...

  3. Leetcode-1.两数之和

    题目描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数 ...

  4. [Swift]LeetCode1 .两数之和 | Two Sum

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  5. LeetCode1.两数之和 JavaScript

    给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 实例: 给定 nums = [2, 7, 11, 15], target ...

  6. Leetcode1.两数之和——简洁易懂

    > 简洁易懂讲清原理,讲不清你来打我~ 输入一个数组和一个整数,从数组中找到两个元素和为这个整数,输出下标![在这里插入图片描述](https://img-blog.csdnimg.cn/img ...

  7. 给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X

    题目:给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X 思路一: 1,先采用归并排序对这个数组排序, 2,然后寻找相邻<k,i>的两数之和sum,找到恰好sum>x的 ...

  8. LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  9. 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 ...

  10. 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. 修改GIT密码

    修改GIT本地密码 控制面板->用户账户和家庭安全->凭证管理器->普通凭证:git:hhtp://*****

  2. linux/centos elasticsearch 环境搭建 安装 运行 使用

    环境搭建也是有些坑的存在,所以整理了一下搭建流程,安全无痛. ElasticSearch是一个开源的分布式搜索引擎,具备高可靠性,支持非常多的企业级搜索用例. 一.java 环境 直接apt安装火箭一 ...

  3. 转发: 关于ST MCU的UID详细说明

    https://www.stmcu.org.cn/article/id-327990 ST MCU芯片中的绝大部分都内置一串96位唯一标识码[unique ID].时不时有人问起这个东西,尤其最近感, ...

  4. MVC的开发模式简单介绍

    MVCM model 模型 实体类和业务和dao dao(数据库访问对象)V view 视图 jspC controller 控制器 servlet 作用:视图和逻辑分离 开发设计顺序 1.设计数据库 ...

  5. 11. java中路径/和\的区别

    一般可以认为是"/"的作用等同于"\\"在java中路径一般用"/"windows中的路径一般用"\"linux.uni ...

  6. 关联tomcat源代码

    1.进入tomcat官网下载对应版本源代码文件 2. 3..ctrl+鼠标左键 点击Cookie对象 4. 5.

  7. 正则简单操作cookie、url search

    正则操作cookie.url getCookie function getCookie(key) { var cookies = window.document.cookie, reg = new R ...

  8. python之路:模块初识

    python王者开发之路:模块初识 模块初识我现在讲的确有点早.不过没关系,后面我会详细说模块. 模块,也就是库,是python三剑客之一.这三剑客,函数.库和类,都是由程序编写而成的.之所以我先说模 ...

  9. Ceph 集群整体迁移方案(转)

    场景介绍:在我们的IDC中,存在着运行了3-6年的Ceph集群的服务器,这些服务器性能和容量等都已经无法满足当前业务的需求,在购入一批高性能机器后,希望将旧机器上的集群整体迁移到新机器上,当然,是保证 ...

  10. 如果测试UI

    1. 先分享几个链接 https://www.ranorex.com/resources/testing-wiki/gui-testing/ https://www.tutorialspoint.co ...