来源

https://leetcode-cn.com/problems/two-sum/description/

题目描述

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

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

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9

所以返回 [0, 1]

代码实现

方法一: 暴力求解

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

方法二:hash table

class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
checked = dict()
for index,item in enumerate(nums):
if item in checked.keys():
return [checked[item], index]
checked[target-item] = index
return [-1, -1]

leetcode NO.1 两数之和 (python实现)的更多相关文章

  1. 【数据结构】Hash表简介及leetcode两数之和python实现

    文章目录 Hash表简介 基本思想 建立步骤 问题 Hash表实现 Hash函数构造 冲突处理方法 leetcode两数之和python实现 题目描述 基于Hash思想的实现 Hash表简介 基本思想 ...

  2. 每日一道 LeetCode (1):两数之和

    引言 前段时间看到一篇刷 LeetCode 的文章,感触很深,我本身自己上大学的时候,没怎么研究过算法这一方面,导致自己直到现在算法都不咋地. 一直有心想填补下自己的这个短板,实际上又一直给自己找理由 ...

  3. Leetcode系列之两数之和

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

  4. leetcode 两数之和 python

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

  5. LeetCode | No.1 两数之和

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

  6. leetcode刷题--两数之和(简单)

    一.序言 第一次刷leetcode的题,之前从来没有刷题然后去面试的概念,直到临近秋招,或许是秋招结束的时候才有这个意识,原来面试是需要刷题的,面试问的问题都是千篇一律的,只要刷够了题就差不多了,当然 ...

  7. LeetCode :1.两数之和 解题报告及算法优化思路

    最近开始重拾算法,在 LeetCode上刷题.顺便也记录下解题报告以及优化思路. 题目链接:1.两数之和 题意 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 ...

  8. LeetCode 167:两数之和 II - 输入有序数组 Two Sum II - Input array is sorted

    公众号: 爱写bug(ID:icodebugs) 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index ...

  9. leetCode刷题 | 两数之和

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

随机推荐

  1. pat甲级1016

    1016 Phone Bills (25)(25 分) A long-distance telephone company charges its customers by the following ...

  2. mongodb 入坑

    一.安装mongodb https://www.mongodb.com/ 官网下载合适的版本,安装在C或者D盘,我选择的是默认路径C:\Program Files\MongoDB\Server\3.4 ...

  3. 访问mongo数据库报错

    It looks like you are trying to access MongoDB over HTTP on the native driver port. 出错原因: 1.没有安装mong ...

  4. IOS UIButton常用属性

    //1.添加按钮 UIButton *nameView=[UIButton buttonWithType:UIButtonTypeCustom]; //nameView.backgroundColor ...

  5. 【51nod1815】调查任务(Tarjan+拓扑)

    点此看题面 大致题意:有\(N\)个城市由\(M\)条单向道路(图不一定联通),每个城市有一个发达程度\(a[i]\),要求你求出首都\(S\)到城市\(i\)的一条路径上的两个不同城市\(x,y\) ...

  6. python读取excel中的数据

    import numpy as np import matplotlib.pyplot as plt import pandas as pd #df = pd.read_excel('/Users/N ...

  7. Luogu [P1958] 上学路线_NOI导刊2009普及(6)

    上学路线_NOI导刊2009普及(6) 题目详见:上学路线_NOI导刊2009普及(6) 这是一道基础的DFS(深搜)题,堪称模板,是新手练习搜索与回溯的好题选. 大致思路:从(1,1)开始搜索,每次 ...

  8. numpy.mean

    http://docs.scipy.org/doc/numpy/reference/generated/numpy.mean.html numpy.mean(a, axis=None, dtype=N ...

  9. 使用FreeMarker导出word文档(支持导出图片)

    一.添加maven依赖,导入FreeMarker所需要的jar包 <dependency> <groupId>org.freemarker</groupId> &l ...

  10. 牛客小白月赛5 A 无关(relationship) 【容斥原理】【数据范围处理】

    题目链接:https://www.nowcoder.com/acm/contest/135/A 题目描述 若一个集合A内所有的元素都不是正整数N的因数,则称N与集合A无关.   给出一个含有k个元素的 ...