Leetcode(1)两数之和

[题目表述]:

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

第一种方法:暴力

执行用时:5352 ms; 内存消耗:12.9MB 效果:非常差

class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
for i in range(0,len(nums)):
for j in range(i+1,len(nums)):
if((nums[i]+nums[j])==target):
return [i,j]

第二种方法:首尾递归查找

执行用时:60 ms; 内存消耗:13.3MB 效果:非常好

class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
sorted_id = sorted(range(len(nums)), key=lambda k: nums[k]) //学到了
head = 0
tail = len(nums) - 1
sum_result = nums[sorted_id[head]] + nums[sorted_id[tail]]
while sum_result != target:
if sum_result > target:
tail -= 1
elif sum_result < target:
head += 1
sum_result = nums[sorted_id[head]] + nums[sorted_id[tail]]
return [sorted_id[head], sorted_id[tail]]

学习

  • sorted排列

    sort与sorted区别:

    sort是应用在list上的方法,sorted可以应用到所有可迭代对象上

    sort返回的是对已存在的列表进行操作,sorted返回的是一个新的list

    sorted(iterable,key,reverse):

    iterable是迭代对象,key缺省则是不以key函数形式进行,有key则意思是按key的函数形式进行排序,reverse=F升序,=T降序,缺省是升序

    => key=lambda 隐函数 这是关键,如上述的key=lambda k: nums[k],意味着range(len(nums))的值

    按照nums[k]的大小,k相当于是迭代器。

  • lambda隐函

  • 算法思想:

    首尾递归,少了就移动头,多了移动尾

第三种方法:字典 / 哈希表

执行用时:56 ms; 内存消耗:13.5MB 效果:最好效果基本上

class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
hashmap = {}
for index, num in enumerate(nums):
another_num = target - num
if another_num in hashmap: //字典中的查找是否存在对应键
return [hashmap[another_num], index]
hashmap[num] = index
return None

学习

  • 字典+枚举 键值对查找

  • enumerate()

    字典上是枚举的意思,对一个可迭代对象,将其组成一个索引列表,利用它同时获得索引和值

    多用于在for循环中得到计数:for index, num in enumerate(nums):

    第二个参数值,用于指定索引起始值

  • hash思想

第四种方法:list查询 / 哈希

执行用时:811 ms; 内存消耗:12.6MB

class Solution:
def twoSum(self, nums, target):
i = 0
while i < len(nums):
if i == len(nums) - 1:
return "No solution here!"
r = target - nums[i]
# Can't use a num twice
num_follow = nums[i + 1:]
if r in num_follow:
return [i, num_follow.index(r) + i + 1]
i = i + 1

学习:

  • if r in num_follow 查询num_follow列表中是否有r这个值

Leetcode(1)两数之和的更多相关文章

  1. 前端与算法 leetcode 1. 两数之和

    目录 # 前端与算法 leetcode 1. 两数之和 题目描述 概要 提示 解析 解法一:暴力法 解法二:HashMap法 算法 传入[1, 2], [11, 1, 2, 3, 2]的运行结果 执行 ...

  2. LeetCode:两数之和、三数之和、四数之和

    LeetCode:两数之和.三数之和.四数之和 多数之和问题,利用哈希集合减少时间复杂度以及多指针收缩窗口的巧妙解法 No.1 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在 ...

  3. Leetcode 001. 两数之和(扩展)

    1.题目要求 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 2.解法一:暴力法(for*for,O(n*n)) ...

  4. LeetCode 653. 两数之和 IV - 输入 BST(Two Sum IV - Input is a BST)

    653. 两数之和 IV - 输入 BST 653. Two Sum IV - Input is a BST 题目描述 给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定 ...

  5. leetCode:twoSum 两数之和 【JAVA实现】

    LeetCode 两数之和 给定一个整数数组,返回两个数字的索引,使它们相加到特定目标. 您可以假设每个输入只有一个解决方案,并且您可能不会两次使用相同的元素. 更多文章查看个人博客 个人博客地址:t ...

  6. Leetcode 1. 两数之和 (Python版)

    有粉丝说我一个学算法的不去做Leetcode是不是浪费,于是今天闲来没事想尝试一下Leetcode,结果果断翻车,第一题没看懂,一直当我看到所有答案的开头都一样的时候,我意识到了我是个铁憨憨,人家是让 ...

  7. Leetcode 167. 两数之和 II - 输入有序数组 By Python

    给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值 ...

  8. LeetCode 167. 两数之和 II - 输入有序数组

    题目: 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的 ...

  9. 【LeetCode】 两数之和 twoSum

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

  10. 【Leetcode】两数之和,三数之和,四数之和

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

随机推荐

  1. [CUDA] 00 - GPU Driver Installation & Concurrency Programming

    前言 对,这是一个高大上的技术,终于要做老崔当年做过的事情了,生活很传奇. 一.主流 GPU 编程接口 1. CUDA 是英伟达公司推出的,专门针对 N 卡进行 GPU 编程的接口.文档资料很齐全,几 ...

  2. .Net Core WebApi(二)在Windows服务器上部署

    上一篇学习到了如何简单的创建.Net Core Api和Swagger使用,既然写了接口,那么就需要部署到服务器上才能够正式使用.服务器主要用到了两种系统,Windows和Linux,.Net和Win ...

  3. jenkins上下游工程以及空间占用处理

    1.最近项目架构调整,把十几个java项目整合为一个大的项目,这样构建上游工程成功后下游工程会自动构建 解决如下:取消这个勾选即可 2.构建单个项目时,会把所有子工程都打包一次 解决如下:指定构建时的 ...

  4. jquery的api以及用法总结-属性/css/位置

    属性/css 属性 .attr() attr()设置普通属性,prop()设置特有属性 获取或者设置匹配的元素集合中的第一个元素的属性的值 如果需要获取或者设置每个单独元素的属性值,需要依靠.each ...

  5. (6)autotools工具的使用

       autotools是专门用来生成Makefile的一系列工具,包括autoscan.aclocal.autoheader.autoconf.automake等.     (1)autotools ...

  6. SpringCloud实现服务间调用(RestTemplate方式)

    上一篇文章<SpringCloud搭建注册中心与服务注册>介绍了注册中心的搭建和服务的注册,本文将介绍下服务消费者调用服务提供者的过程. 本文目录 一.服务调用流程二.服务提供者三.服务消 ...

  7. Spring boot 梳理 - 显示Springboot默认自动生成的bean

    @Autowired public ApplicationContext context; @Bean public ViewResolver freeMarkerViewResolver(){ St ...

  8. springboot 集成Redis一主二从三哨兵

    1.Centos7 Redis一主二从三哨兵配置 Redis一主二从三哨兵环境搭建 2.接入过程 与集成redis单机不同的是jedis相关的配置做了修改,JedisPool换成了JedisSenti ...

  9. FastDfs之TrackerServer的详细配置介绍

    # is this config file disabled # false for enabled # true for disabled disabled=false #当前配置是否不可用fals ...

  10. Nodejs 发送邮件 激活邮箱

    1. 安装nodemailer npm install nodemailer 项目中引入nodemailer var nodemailer = require('nodemailer'); 2.QQ邮 ...