1.遍历一次nums,利用map记录已经遍历过的nums[i],并记录其index :i+1

2.每次求出b=target-nums[i],检测b是否在map中,如果在,则已经得到答案,否则,把nums[i]存到map里面

AC代码:

class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int,int> exist;
vector<int> ans(0);
for(int i=0;i<nums.size();i++)
{
int b=target-nums[i];
if(exist.find(b)!=exist.end())
{//如果哈希表里面已经存在b,则压入ans中,并return,b先于nums[i]放入哈希表,所以b的index肯定小于nums[i]
ans.push_back(exist[b]);
ans.push_back(i+1);
return ans;
}
else
{//找不到,即和不为target,则把nums[i]放入哈希表
exist[nums[i]]=i+1;
}
} }
};

Two Sum(Medium)的更多相关文章

  1. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  2. 【leetcode】Binary Tree Maximum Path Sum (medium)

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  3. 39. Combination Sum(medium, backtrack 的经典应用, 重要)

    Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...

  4. [leetcode] 64. Minimum Path Sum (medium)

    原题 简单动态规划 重点是:grid[i][j] += min(grid[i][j - 1], grid[i - 1][j]); class Solution { public: int minPat ...

  5. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  6. 【目录】Leetcode

    Leetcode 1.动态规划 Palindrome Partitioning II(hard) ☆ Distinct Subsequences(hard) Edit Distance (hard) ...

  7. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  8. leetcode算法总结

    算法思想 二分查找 贪心思想 双指针 排序 快速选择 堆排序 桶排序 搜索 BFS DFS Backtracking 分治 动态规划 分割整数 矩阵路径 斐波那契数列 最长递增子序列 最长公共子系列 ...

  9. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

随机推荐

  1. SQL优化——ORACLE

    SQL优化——ORACLE 索引是由Oracle维护的可选结构,为数据提供快速的访问.准确地判断在什么地方需要使用索引是困难的,使用索引有利于调节检索速度. 当建立一个索引时,必须指定用于跟踪的表名以 ...

  2. 汪慧和201771010123《面向对象程序设计(Java)》第三周学习总结

    1.实验目的与要求 (1)进一步掌握Eclipse集成开发环境下java程序开发基本步骤: (2)熟悉PTA平台线上测试环境: (3)掌握Java语言构造基本程序语法知识(ch1-ch3): (4)利 ...

  3. @Data与@ConfigurationProperties 简化配置属性数据

    参考地址:https://www.cnblogs.com/FraserYu/p/11261916.html   在编写项目代码时,我们要求更灵活的配置,更好的模块化整合.在 Spring Boot 项 ...

  4. 同时运行两个版本相同的tomcat

    由于项目需要,代理集群和一个节点都部署在本地,那么就需要有两个tomcat,一个部署集群,一个部署项目,我都用了7.0.34版本的tomcat 当启动代理的tomcat成功时,再启动节点的tomcat ...

  5. 漫谈设计模式(三):桥接(Bridge)模式 —— 将类功能、结构两层次分离

    1.前言 类主要有两个层次,一个是功能层次,另一个是实现层次. 功能层次,一般应用于当前类不能满足多样化的业务需求,让子类去继承(具体)父类,添加加一些父类中没有的功能(一般是增加新的方法),这就属于 ...

  6. placeholder在IE下的兼容问题

    最近写项目要求兼容到ie8,写完了去ie测试的时候,发现了placeholder在ie下的兼容问题,为了解决,搜罗网上各种牛人的解决方案,自己总结如下: css样式(设置各浏览器下placeholde ...

  7. 关于mysql一边查一边更新

    update test_table set user_id = 112 where id in (select id from ( select id from test_table where nu ...

  8. UVa202

    刚刚开始写的适合感觉是转换成字符然后开始遍历一遍,后面发现各种不行,就回去看了看题目,重新构思,写了好久还是WA,最后只能看下大神的操作(我太菜了). 先简单梳理下题目意思:首先给出两个数,然后这两个 ...

  9. Maven打包时报Failed to execute goal org.apache.maven.plugins:maven-war-plugin:解决方案

    问题现象: 用Maven打包时,报Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war错误. 原因分析: 打 ...

  10. 第04项目:淘淘商城(SpringMvc+Spring+Mybatis) 的学习实践总结【第三天】

    淘淘商城(SpringMVC+Spring+Mybatis)  是传智播客在2015年9月份录制的,几年过去了.由于视频里课上老师敲的代码和项目笔记有些细节上存在出入,只有根据日志报错信息作出适当的调 ...