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 ...
随机推荐
- 对map集合按照value从大到小进行排序
概述: 基本特点: 该集合存储键值对,而且要保证键的惟一性 子类: |--HashTable 底层是哈希数据表结构,不可以使用Null作为键或者值:该集合线程是同步的 |--hashMap 底层是 ...
- 获取物理内存total值和used值
1.使用 free -m 查看 2.物理内存total值 # free -m | grep Mem | awk '{print $2}' 3.物理内存used值 # free -m | grep Me ...
- java.lang.NoSuchMethodException: tk.mybatis.mapper.provider.base.BaseSelectProvider.<init>()
这个问题好奇怪, 出现这个错误是通用Mapper初始化的错误,排查的方向就是往这个方向,可能的情况有以下几种: .jar包冲突 <dependency> <groupId>tk ...
- ssm注入失败
今天做ssm整合时候,创建bean/注入一直出错,检查几遍没发现问题,后来发现犯了个低级错误,mapper.xml的<mapper namespace="XXXXX" > ...
- Java数组操作十大方法 (转)
定义一个Java数组 String[] aArray = new String[5]; String[] bArray = {"a","b","c&q ...
- Linux 总线、设备、驱动模型 与 设备树
1.总线.设备.驱动模型 本着高内聚.低耦合的原则,Linux 把设备驱动模型分为了总线.设备和驱动三个实体,这三个实体在内核里的职责分别如下: 设备和驱动向总线进行注册,总线负责把设备和对应的驱动绑 ...
- 正则简单操作cookie、url search
正则操作cookie.url getCookie function getCookie(key) { var cookies = window.document.cookie, reg = new R ...
- Angular开发环境构筑
今天按照下面的顺序构筑了Angular的开发环境.很简单. -- 系统:win7, 64位 1.安装Note 从<https://nodejs.org/ja/>下载安装文件,安装. Not ...
- SSIS: 如何通过SSIS的Foreach Loop Container导入Excel的多个Sheet
通常都有这样一个习惯就是按月存放我们的一些数据,一个月份一个work sheet,他们具有相同的行列组合,假设有下面这样一个Excel,每页结构如下,共有四页,每页为一个季度 那么我们需要做的是: 1 ...
- [SF] Symfony 标准 HttpFoundation\Request 实现分析
使用方式 /** * 如果直接示例化 Request 默认是没有参数的,可以自己传入 * 本方法将 PHP 超全局变量作为参数然后实例化自身(Request)进行初始化. */ $request = ...