Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

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

You may assume that each input would have exactly one solution, and you may not use the same element twice.

这道题穷举法时间复杂度o(n2)太高了,通过转换为减法利用字典查找数字可以更快的完成。字典初始化和查找可合并成一个循环。另一种想法是对给定数组进行排序,首尾不断相加,直到找到指定的和。

class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
dict={}
for i , num in enumerate(nums):
if(target-num)in dict:
return(dict[target-num],i)
else:
dict[num] = i

[LeetCode]题1:two sum的更多相关文章

  1. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  2. 【一天一道LeetCode】#113. Path Sum II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  3. 【一天一道LeetCode】#40. Combination Sum II

    一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...

  4. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  5. leetcode 练习1 two sum

    leetcode 练习1  two sum whowhoha@outlook.com 问题描述 Given an array of integers, return indices of the tw ...

  6. [LeetCode] Minimum Size Subarray Sum 解题思路

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  7. leetcode题库

    leetcode题库 #题名题解通过率难度出现频率  1 两数之和     46.5%简单2 两数相加     35.5%中等3 无重复字符的最长子串     31.1%中等4 寻找两个有序数组的中位 ...

  8. 简单的leetcode题

    简单的leetcode题 环绕字符串中唯一的子字符串 把字符串 s 看作是\("abcdefghijklmnopqrstuvwxyz"\)的无限环绕字符串,所以 s 看起来是这样的 ...

  9. 【LeetCode】813. Largest Sum of Averages 解题报告(Python)

    [LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  10. Kotlin实现LeetCode算法题之Two Sum

    LeetCode介绍 LeetCode是算法练习.交流等多功能网站,感兴趣的同学可以关注下(老司机请超车).页面顶部的Problems菜单对应算法题库,附带历史通过滤.难易程度等信息. 未来计划 打算 ...

随机推荐

  1. TestFlight 的使用记载

    TestFlight:     TestFlight内测网上很多资料.大概是先打包,然后在App Store Connect 里添加测试员的邮箱地址. Testflight.top:公测要用到test ...

  2. java框架之SpringBoot(15)-安全及整合SpringSecurity

    SpringSecurity介绍 Spring Security 是针对 Spring 项目的安全框架,也是 Spring Boot 底层安全模块默认的技术选型.它可以实现强大的 Web 安全控制.对 ...

  3. js回溯法计算最佳旅行线路

    假如有 A,B,C,D四个城市,他们之间的距离用 G[V][E] 表示,为 无穷大,则表示两座城市不相通 现在从计算从某一个城市出发,把所有的城市不重复旅行一次,最短路径 其中G为: (Infinit ...

  4. 原生侧边栏sidebar

    创建侧栏导航 html: <a href="#" class="btn">点我啊</a> <div class="sid ...

  5. Oracle 24角色管理

    了解什么是角色 Oracle角色(role)就是一组权限(或者说是权限的集合). 用户可以给角色赋予指定的权限,然后将角色赋给相应的用户. 三种标准的角色 connect(连接角色) 拥有connec ...

  6. 关于UI适配的文档

    第一部分:原理 (1)根据当前屏幕尺寸与开发预设屏幕尺寸尺寸得出以下参数. 1 XRatio:当前屏幕尺寸与开发尺寸的X轴比例 2 YRtaio:当前屏幕尺寸与开发尺寸的Y轴比例 3minRatio: ...

  7. Eclipse 在启动发生错误异常:An internal error occurred during: "Initializing Java Tooling". java.lang.NullPointerException的解决办法

    异常现象: 打开Eclipse,出现 An internal error occurred during: "Initializing Java Tooling". java.la ...

  8. flask重要点

    django与flask的区别 django: 大而全的框架,包含了很多组件,例如:ORM.form.ModelForm.session... flask: 轻量级的可扩展强的框架.有丰富的第三方组件 ...

  9. Gradle引人注目的特性集

    Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化建构工具.它使用一种基于Groovy的特定领域语言来声明项目设置,而不是传统的XML.当前其支持的语言限于Java. ...

  10. MongoDB集群单mongos的问题总结

    问题发现 在使用过程中,通过spark访问集群的效率不是很令人满意,80核心同时运行的速度比单核心也就快了20倍左右,预测瓶颈在mongodb读写上.当然,此时没遇到其他问题暂时没进行问题梳理. 在数 ...