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 ...
随机推荐
- BZOJ 3524 [Poi2014]Couriers(可持久化线段树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3524 [题目大意] 给一个长度为n的序列a.1≤a[i]≤n. m组询问,每次询问一个 ...
- LongPathException问题解析
一.背景 当windows系统下使用System.IO命名空间下的方法,目录长度超过260个字符时,.net framework会抛出LongPathException.查阅相关资料,发现是 ...
- java实现发送邮件功能
项目中实现发送邮件功能,先书写一个小Demo,记录如下: POM.XML中导入依赖 <!-- start java 提供的支持邮件发送相关业务的类 --> <dependency&g ...
- RMAN备份恢复 控制文件和归档日志丢失情况
RMAN> backup current controlfile tag='bak_ctlfile' format='/home/oracle/backup/bak_ctl_%U_%T'; al ...
- MVC架构、WebForm与MVC对比
ylbtech-ASP.NET MVC:WebForm与MVC对比 功能描述:WebForm与MVC对比 A.1,MVC架构 •MVC(Model-View-Controller)用于表示一种软件架构 ...
- (原创)sklearn中 F1-micro 与 F1-macro区别和计算原理
最近在使用sklearn做分类时候,用到metrics中的评价函数,其中有一个非常重要的评价函数是F1值,(关于这个值的原理自行google或者百度) 在sklearn中的计算F1的函数为 f1_sc ...
- 用 JavaScript 检测浏览器在线/离线状态(JavaScript API — navigator.onLine)
如今HTML5 移动应用或 Web app 中越来越普遍的使用了离线浏览技术,所以用 JavaScript 检测浏览器在线/离线状态非常常见. 无论浏览器是否在线,navigator.onLine 属 ...
- http://blog.163.com/eugeneheen_chen@126/blog/static/120812157201291994916866/
http://blog.163.com/eugeneheen_chen@126/blog/static/120812157201291994916866/
- yii2 URL重写 nginx的配置
Url的重写 nginx的配置文件 [root@localhost protected]# vim /etc/nginx/conf.d/default.conf server { listen ...
- 基于Spark Mllib,SparkSQL的电影推荐系统
本文测试的Spark版本是1.3.1 本文将在Spark集群上搭建一个简单的小型的电影推荐系统,以为之后的完整项目做铺垫和知识积累 整个系统的工作流程描述如下: 1.某电影网站拥有可观的电影资源和用户 ...