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 ...
随机推荐
- Problem V: 零起点学算法20——输出特殊值II
#include<stdio.h> int main() { printf("\\n"); ; }
- 十. 图形界面(GUI)设计10.菜单
有两种类型的菜单:下拉式菜单和弹出式菜单.本章只讨论下拉式菜单编程方法.菜单与JComboBox和JCheckBox不同,它们在界面中是一直可见的.菜单与JComboBox的相同之处是每次只可选择一个 ...
- Saga alternatives – routing slips
In the last few posts on sagas, we looked at a variety of patterns of modeling long-running business ...
- react项目如何调试?
进入到相应的网页界面,然后查看Sources->Page->top->webpack://->找到react的js代码处,设置断点,进行调试
- Makefile的制作
一个工程中的源文件不计其数,其按类型.功能.模块分别放在若干个目录中,makefile定义系列的规则来指定,哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译,甚至于进行更复杂的功能操作,因 ...
- nagios系列教程地址
http://www.sosidc.com/sort/10/page/3 http://www.sosidc.com/sort/10/page/2 http://www.sosidc.com/sort ...
- EL表达式介绍(2)
1. EL关系运算符: 关系运算符 说明 范例 结果 == 或 eq 等于 ${5==5}或${5eq5} true != 或 ne 不等于 ${5!=5}或${5ne5} false < 或 ...
- IBM AppScan安全測试一例——已解密的登录请求
问题严重级别:高 此类问题在做政府项目(第三方软件评測中心)验收的时,须要马上整改.例如以下图:
- 使用Visual Studio的动态连接库创建通用数据库连接对话框
1.在VS(此处文件夹文件以vs2010为例)安装文件夹下("%Visual Studio安装文件夹%/Common10/IDE/Microsoft.Data.ConnectionUI.Di ...
- Quartz定时框架入门
Quartz框架是Java开源的定时任务调度器,Quartz框架中有如下核心概念: 1. Job 任务接口,接口中只声明方法void execute(JobExecutionContext conte ...