描述:

给个数组和整数t,一串整数中,存在两个数其和等于t,求这两个数的索引。

解决:

想要快,用个哈希储存曾经出现过的数的索引。

vector<int> twoSum(vector<int>& nums, int target) {
int size = nums.size();
unordered_map<int, int> id; for (int i = ; i < size; i++) {
int o = target - nums[i];
auto fi = id.find(o);
if (fi != id.end()) {
vector<int> ret = {fi->second, i};
return ret;
}
id.insert(make_pair(nums[i], i));
}
return vector<int>();
}

leetcode 1 A+B problems的更多相关文章

  1. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  2. Leetcode Elemination Game

    题目网址:https://leetcode.com/contest/2/problems/elimination-game/ 题意: 给定一个从1到n的数列,第一次从最左边开始,每隔一个淘汰一个数字. ...

  3. [leetcode] 407. Trapping Rain Water II

    https://leetcode.com/contest/6/problems/trapping-rain-water-ii/ 看到这题,我很高兴,因为我做过!哈哈!其实我现在也写不出来,知道大概思想 ...

  4. [leetcode] 406. Queue Reconstruction by Height

    https://leetcode.com/contest/6/problems/queue-reconstruction-by-height/ 分析:每个表示成(a,b)的形式,其实找第一个,就是b为 ...

  5. [leetcode] 405. Convert a Number to Hexadecimal

    https://leetcode.com/contest/6/problems/convert-a-number-to-hexadecimal/ 分析:10进制转换成16进制,不能用库函数,刚开始,我 ...

  6. [leetcode] 403. Frog Jump

    https://leetcode.com/contest/5/problems/frog-jump/ 这个题目,还是有套路的,之前做过一道题,好像是贪心性质,就是每次可以跳多远,最后问能不能跳到最右边 ...

  7. [leetcode] 401. Binary Watch

    https://leetcode.com/contest/5/problems/binary-watch/ 这个题应该是这次最水的,这个题目就是二进制,然后是10位,最大1024,然后遍历,找二进制里 ...

  8. [leetcode] 400. Nth Digit

    https://leetcode.com/contest/5/problems/nth-digit/ 刚开始看不懂题意,后来才理解是这个序列连起来的,看一下第几位是几.然后就是数,1位数几个,2位数几 ...

  9. LeetCode题解 | 215. 数组中的第K个最大元素

    在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2 输出: 5 ...

随机推荐

  1. Nginx 静态资源缓存配置

    示例 # Media: images, icons, video, audio, HTC location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|m ...

  2. WIN7下用笔记本创建无线网

    系统要求:WIN7  硬件要求:拥有无线网卡  网络要求:已经连接internet 1.启用并设定虚拟Wifi网卡:运行以下命令启用虚拟无线网卡(相当于打开路由器): netsh wlan set h ...

  3. 解决maven install报错信息(Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile )

    Maven install失败 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (d ...

  4. 第11课 enum,sizeof,typedef分析

    枚举类型的使用方法: enum的地位和struct是相等的,enum主要用来根据需要定义一些离散的值. 枚举类型的特殊意义: 工程中常用无名枚举来定义常量. 程序示例: #include <st ...

  5. PHP write byte array to file

    /********************************************************************************* * PHP write byte ...

  6. 20145237《网络攻防》Web基础

    基础问题回答 什么是表单: 表单是一个包含表单元素的区域.表单元素是允许用户在表单中输入信息的元素.表单在网页中主要负责数据采集功能. 浏览器可以解析运行什么语言: 超文本标记语言:HTML: 可扩展 ...

  7. iis6 , URL重写HTM文件名后,出现真实的HTM文件不能访问的解决

    服务器环境是windows 2003 IIS6 在web.config文件中加入 1.在<compilation debug="true"> 节点加入 <buil ...

  8. 报表生成poi----java操作java对象生成execl表单

    1.Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. .NET的开发人员则 ...

  9. hasura graphql schema 导出

    使用的是apollo 的插件 安装apollo npm install -g apollo 基本使用 因为我使用了模式拼接,所以地址有变动,一般是 http://host:port/v1alpha1/ ...

  10. Linux环境安装jdk10

    一. 下载jdk 下载方式一:直接在linux上下载 wget --no-check-certificate --no-cookies --header "Cookie: oraclelic ...