Part 1. 题目描述 (easy)

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.

Example:

Given nums = [2, 7, 11, 15], target = 9, because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].

Part 2. 分析

本题是LeetCode的第一题,求解题目并不难,但是如何进一步优化时间复杂度是本题的重点。需要用到的基础是hash table,在python中可以用字典来实现。

Part 3. 解决方案

【方法1: 暴力求解 (Brute Force)】

最简单直接的求解方式,遍历所有两个数的组合,将两数之和跟target的值进行比较。时间复杂度O(n2),空间复杂度O(1)。

python 代码如下:

 class Solution:
def twoSum(self, nums, target):
for i in range(len(nums)):
for j in range(i+1, len(nums)):
if nums[i] + nums[j] == target:
return [i, j]

【方法2: One-pass Hash Table】

如果我们想要降低时间复杂度,该如何解决呢?我们可以借助hash函数来实现,因为它可以免除了第二轮的遍历过程,使搜索时间变为O(1),总的时间复杂度降低为O(n)。但相应的,其空间复杂度会变复杂,变为O(n)。

python 代码如下:

 class Solution:
def twoSum(self, nums, target):
dict_nums = {} # key: num values: index
for i in range(len(nums)):
rest = target - nums[i]
if rest in dict_nums:
return [dict_nums[rest], i]
dict_nums[nums[i]] = i

备注:注意判断hash中是否存在需要的数字(line 5-7)和添加新数字到hash中(line 8)的顺序,如果反过来写,某些case会出错,比如nums = [3,3,1,4], target = 6. 因为在hash中,我们将数字作为key来存储,这要求key是唯一的。如果我们先执行存储操作的话,当出现2个相同数字的时候就会报错。

Part 4. 心得体会 

刚开始接触LeetCode,想通过刷算法题的方法把算法、数据结构的基础知识好好巩固一番。因为主要不是为了面试而刷题,所以做题顺序上可以更随心所欲一些。准备先做top 100的题目,然后其余的题目顺序待定。编程语言准备以python为主,虽然java、C++都用过,但是都没有到熟练掌握的程度。因为以后可能更多的会用python来做实验,所以正好这回多写点python程序,提升代码水平,希望自己能坚持下去咯!

完事开头难,这一题虽然是easy级别的,但是自己在第一次写暴力求解代码的时候还是粗心出了错,脑子有点跟不上节奏啊......在学习方法2的时候,因为对python字典不怎么了解,还花时间去学习了字典的基本操作。再加上这是我第一次在博客上写技术的东西(以前都是私底下用有道笔记),所以花了不少时间,但是已经从中感受到乐趣啦。

[LeetCode] 1. Two Sum 两数之和的更多相关文章

  1. [LeetCode]1.Two Sum 两数之和&&第一次刷题感想

    ---恢复内容开始--- 参考博客: https://www.cnblogs.com/grandyang/p/4130379.html https://blog.csdn.net/weixin_387 ...

  2. [LeetCode] 1.Two Sum 两数之和分析以及实现 (golang)

    题目描述: /* Given an array of integers, return indices of the two numbers such that they add up to a sp ...

  3. 【LeetCode】Two Sum(两数之和)

    这道题是LeetCode里的第1道题. 题目描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会 ...

  4. [leetcode]1. Two Sum两数之和

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

  5. [LeetCode]1.Two Sum 两数之和(Java)

    原题地址:two-sum 题目描述: 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target  的那 两个 整数,并返回它们的数组下标. 你可以假设每 ...

  6. LeetCode刷题 1. Two Sum 两数之和 详解 C++语言实现 java语言实现

    1. Two Sum 两数之和 Given an array of integers, return indices of the two numbers such that they add up ...

  7. 【LeetCode】1. Two Sum 两数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:two sum, 两数之和,题解,leetcode, 力 ...

  8. Leetcode:0002(两数之和)

    LeetCode:0002(两数之和) 题目描述:给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表.你可以假设除了数字 0 之外,这两 ...

  9. Leetcode(1)两数之和

    Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...

随机推荐

  1. TCSL:遇到网络正常,但是添加网口打印机总是失效的问题。

    1. 环境 这家店要换成TCSL餐饮系统,但是店主希望在换系统时候,保持原来系统正常运转.所以,一开始踩点和实施都是小心翼翼~~ 不过,还是遇到问题,没法打印,如果开启TCSL打印服务,就会和原来的餐 ...

  2. linux操作命令之压缩命令

    常用的压缩格式:    .zip     .gz     .bz2 一..zip格式压缩 zip 压缩文件名 源文件 压缩文件 zip -r 压缩文件名 源目录 压缩目录 解压缩 unzip 压缩文件 ...

  3. Raft协议实战之Redis Sentinel的选举Leader源码解析

    这可能是我看过的写的最详细的关于redis 选举的文章了, 原文链接 Raft协议是用来解决分布式系统一致性问题的协议,在很长一段时间,Paxos被认为是解决分布式系统一致性的代名词.但是Paxos难 ...

  4. netsh winsock reset命令

    公司一台电脑无法浏览网页,其他基本正常,鼓捣了一个多小时,依然无法解决.. 一开始按照正常思路,感觉是dns的问题,查看了下DNS,真是自定义的,于是改成自动获取,无效 重启了网卡,无效 重启电脑,无 ...

  5. Shiro在SSM框架中的应用

    上一篇Shiro基础的连接 如果想使用Relam的操作,那么必须要保证有一个具体的认证类实现了Relam接口 web.xml增加shiro的配置 <!-- 进行shiro的过滤器的配置 --&g ...

  6. 事件派发dispatchEvent

    1.什么是dispatchEvent? dispatch意为"调度"."派遣",event为"事件".所以dispatchEvent即向指定 ...

  7. [Swift]LeetCode383. 赎金信 | Ransom Note

    Given an arbitrary ransom note string and another string containing letters from all the magazines, ...

  8. [Swift]LeetCode924.尽量减少恶意软件的传播 | Minimize Malware Spread

    In a network of nodes, each node i is directly connected to another node j if and only if graph[i][j ...

  9. [Swift]LeetCode997. 找到小镇的法官 | Find the Town Judge

    In a town, there are N people labelled from 1 to N.  There is a rumor that one of these people is se ...

  10. Android-线程池下载多个图片并保存,如果本地有该图,则不下载,直接展示到view

    做了个工具方法,用来下载图片,如果本地有这个图,则不下载,直接展示到view setHP()方法可以多次使用,因为使用了线程池,所以是个异步操作,如果使用的多,建议根据需要增加线程池的线程数量 看代码 ...