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】的更多相关文章
- 56. Two Sum【easy】
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- 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 ...
- 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 ...
- 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 ...
- 661. Image Smoother【easy】
661. Image Smoother[easy] Given a 2D integer matrix M representing the gray scale of an image, you n ...
- 485. Max Consecutive Ones【easy】
485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...
- 561. Array Partition I【easy】
561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...
- 2. Trailing Zeros【easy】
2. Trailing Zeros[easy] Write an algorithm which computes the number of trailing zeros in n factoria ...
- 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 ...
随机推荐
- POJ 3260 The Fewest Coins(背包问题)
[题目链接] http://poj.org/problem?id=3260 [题目大意] 给出你拥有的货币种类和每种的数量,商店拥有的货币数量是无限的, 问你买一个价值为m的物品,最少的货币流通数量为 ...
- 【并查集】bzoj1015 [JSOI2008]星球大战starwar
倒着处理删点,就变成了加点,于是并查集. #include<cstdio> using namespace std; #define N 400001 int fa[N],kill[N], ...
- [OpenJudge8786][暴力DP]方格取数
方格取数 总时间限制: 1000ms 内存限制: 65536kB [描述] 设有N*N的方格图(N<=10),我们将其中的某些方格中填入正整数,而其他的方格中则放入数字0.如下图所示(见样例): ...
- no such file or directory : 'users/shikx/xxx/xxx/Appirater.m'
删除此处红色的.m文件即可
- RxJava 2.x 理解-1
在RxJava 1.x 系列中,讲解了RxJava的大致用法,因为现在都用RxJava 2了,所以Rxjava 1就不细讲,主要来学习RxJava 2. 基本使用: /** * rajava2 的基本 ...
- PHP将字符串首字母大小写转换
每个单词的首字母转换为大写:ucwords() <?php $foo = 'hello world!'; $foo = ucwords($foo); // Hello World! $bar = ...
- MySQL第三方客户端工具
如前所述,MySQL是一个基于客户机--服务器的DBMS,因此,为了使用MySQl,你需要有一个客户机软件给MySQL提供要执行的命令.即你需要一个编写和测试MySQL脚本的工具. 1.MySQL命令 ...
- 【SQL】查询数据库中某个字段有重复值出现的信息
select name,mobile from [GeneShop].[dbo].[xx_member] where mobile in ( SELECT mobile FROM [GeneShop] ...
- Ado.Net基础拾遗二:插入,更新,删除数据
插入数据 public void InsertDataToSQL() { string conStr = ConfigurationManager.ConnectionStrings["No ...
- telnet命令
详细资料 telnet命令使用方法详解-telnet命令怎么用-win7没有telent怎么办 2017年07月26日 15:37:36 阅读数:1010 什么是Telnet? 对于Telnet的认识 ...