[LeetCode]题1:two sum
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的更多相关文章
- LeetCode 2 Add Two Sum 解题报告
LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...
- 【一天一道LeetCode】#113. Path Sum II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【一天一道LeetCode】#40. Combination Sum II
一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- leetcode 练习1 two sum
leetcode 练习1 two sum whowhoha@outlook.com 问题描述 Given an array of integers, return indices of the tw ...
- [LeetCode] Minimum Size Subarray Sum 解题思路
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- leetcode题库
leetcode题库 #题名题解通过率难度出现频率 1 两数之和 46.5%简单2 两数相加 35.5%中等3 无重复字符的最长子串 31.1%中等4 寻找两个有序数组的中位 ...
- 简单的leetcode题
简单的leetcode题 环绕字符串中唯一的子字符串 把字符串 s 看作是\("abcdefghijklmnopqrstuvwxyz"\)的无限环绕字符串,所以 s 看起来是这样的 ...
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- Kotlin实现LeetCode算法题之Two Sum
LeetCode介绍 LeetCode是算法练习.交流等多功能网站,感兴趣的同学可以关注下(老司机请超车).页面顶部的Problems菜单对应算法题库,附带历史通过滤.难易程度等信息. 未来计划 打算 ...
随机推荐
- 简单的可以跑起来的dubbo例子
原文地址:https://blog.csdn.net/jingyangV587/article/details/78901937 项目结构: maven项目,内部三个module. <?xml ...
- python摸爬滚打之day26----网络编程之socket
1.网络通信原理 互联网的本质就是一系列的网络协议, 统称为互联网协议. 互联网协议的功能:定义计算机如何接入internet,以及接入internet的计算机通信的标准. 互联网协议按照功能不同分为 ...
- 颜色模式、DPI和PPI、位图和矢量图
颜色模式:用于显示和打印图像的颜色模型 RGB:电子设备的颜色 CMYF:印刷的颜色 印刷的图像分辨率大于等于120像素/厘米,300像素每英寸 图像分辨率单位为PPI(每英寸像素Pixel per ...
- reactjs中使用高德地图计算两个经纬度之间的距离
第一步下载依赖 npm install --save react-amap 第二步,在组件中使用 import React, { Component } from 'react' import { L ...
- matlab之导入txt文件并取其中一列数据
1.我想导入下面这个文件的内容,并且获取这个文件中的两列数据. 2.首先确保Matlab当前所在文件夹为txt文件所在文件夹,然后命令行执行: X = load('2019_03_21_08_59_0 ...
- 第十篇——Struts2的拦截器栈
拦截器栈: 从结构上看:拦截器栈相当于多个拦截器的组合: 从功能上看:拦截器栈也是拦截器. 默认拦截器栈: 在struts-core.jar包中的struts-default.xml中自定义了一个de ...
- springmvc sessionfilter 登录过滤器
1.在web.xml中配置 <!-- sessionfilter --> <filter> <filter-name>sessionFilter</filte ...
- hihoCoder 1394 : 网络流四·最小路径覆盖
题目链接:https://hihocoder.com/problemset/problem/1394 题目说是网络流,但是其实就是求有向无环图的最小路径覆盖. 不会网络流,只好用二分匹配了. 把每个点 ...
- ESP8266使用笔记之常用固件
开发板使用的是NodeMCU开发板: 目录 1.学习使用ESP8266官方的SDK 1.1使用SDK提供的AT固件 1.2使用SDK Build固件 2.学习使用Nod ...
- Shell 解释器初识
1.脚本文件要以.sh结尾,第一行要跟#!/bin/bash解释器. 2.运行shell脚本. (1)添加权限:可以加x执行权限,./123.sh (2)命令执行:bash 123.sh,sh 123 ...