1. Two Sum【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].

解法一:

 class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> u_map;
vector<int> result; for (int i = ; i < nums.size(); ++i)
{
if (u_map.find(target - nums[i]) != u_map.end())
{
result.push_back(u_map[target - nums[i]]);
result.push_back(i);
return result;
} u_map.insert(make_pair(nums[i], i));
} return result;
}
};

map搞一把

解法二:

 class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> map;
int n = (int)nums.size();
for (int i = ; i < n; i++) {
auto p = map.find(target - nums[i]);
if (p != map.end()) {
return {p->second, i};
}
map[nums[i]] = i;
}
}
};

比较简洁的写法

扩展见:

167. Two Sum II - Input array is sorted【easy】

170. Two Sum III - Data structure design【easy】

												

1. Two Sum【easy】的更多相关文章

  1. 56. Two Sum【easy】

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  2. 170. Two Sum III - Data structure design【easy】

    170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...

  3. 167. Two Sum II - Input array is sorted【easy】

    167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...

  4. 121. Best Time to Buy and Sell Stock【easy】

    121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...

  5. 661. Image Smoother【easy】

    661. Image Smoother[easy] Given a 2D integer matrix M representing the gray scale of an image, you n ...

  6. 485. Max Consecutive Ones【easy】

    485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...

  7. 561. Array Partition I【easy】

    561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...

  8. 2. Trailing Zeros【easy】

    2. Trailing Zeros[easy] Write an algorithm which computes the number of trailing zeros in n factoria ...

  9. 160. Intersection of Two Linked Lists【easy】

    160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...

随机推荐

  1. java 中==符号的坑

    在某技术群看到这样的一个面试题目: 这是一个4年经验的java 从业者的答案. 你的答案是什么呢? 正确的答案是true. 为什么? 其实当使用String a="a"+" ...

  2. Java高级架构师(一)第21节:通过X-gen生成商品模块

    package com.sishuok.architecture1.goodsmgr.vo; import com.sishuok.architecture1.common.vo.BaseModel; ...

  3. Mac Sublime Text 3 配置Python环境及安装插件

    一.下载安装Sublime Text 3 官网下载地址:http://www.sublimetext.com/3 二.配置Python开发环境 1.点击右下角,选择python 2.添加编译环境pyt ...

  4. [HTML/CSS]uploadify自定义按钮样式

    概述 在项目中经常用到uploadify上传插件,但是FLASH按钮的外观往往跟我们网页的设计的主题色不太搭配.这时就需要对其样式进行修改. 样式文件是uploadify.css. 打开这个文件后,你 ...

  5. 项目管理利器——Maven阅读目录

    阅读目录 一.Maven介绍及环境搭建 二.构建Maven版的Hello World 三.Maven常见构建命令 四.自动创建目录骨架 五.Maven中的坐标和仓库 六.在eclipse中安装Mave ...

  6. Discuz! 6.x/7.x 版本 前台任意代码执行漏洞

    一.漏洞原理: 由于php5.3.x版本里php.ini的设置里request_order默认值为GP,导致Discuz! 6.x/7.x 全局变量防御绕过漏洞. include/global.fun ...

  7. 配置SSH单向无密码访问

    服务器架构:系统:CentOS 6.5 x64主控端A:192.168.0.150远端主机B:192.168.0.151 原理: 利用ssh key生成公钥.私钥,密钥相当于一把钥匙,而公钥就相当于一 ...

  8. csharp 面向对象编程

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Shap ...

  9. javascript快速入门26--XPath

    XPath 简介 XPath 是一门在 XML 文档中查找信息的语言.XPath 可用来在 XML 文档中对元素和属性进行遍历.XPath 是 W3C XSLT 标准的主要元素,并且 XQuery 和 ...

  10. javascript 正则表达式判断只能是中文、英文或者中文加英文

    var reglx =/^[\u4e00-\u9fa5a-zA-Z]+$/ 这个是至少有一个中文或者英文 var reglx =/^[\u4e00-\u9fa5a-zA-Z]*$/ 这个是0个以上的中 ...