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. [BZOJ 1789] Necklace

    Link: BZOJ 1789 传送门 Solution: 感觉$n\le 50$可以随便乱搞啊…… 这里我是先找到3条链的失配位置,再找到这之后其中2条链最远的失配位置,统计即可 Code: #in ...

  2. [Android]Android 布局中如何让图片和文字居中显示?

    图片文字居中显示 **①组件TextView的属性 drawableTop ``` <LinearLayout android:layout_width="match_parent&q ...

  3. SpingMVC实现操作的第一方式

    (一) 使用SpringMVC框架的步骤 (1):在web.xml中配置前端控制器 (2):在Spring-servlet.xml中配置 配置处理器映射器HandlerMapping(处理器Handl ...

  4. nativeexcel数据集导出excel

    nativeexcel数据集导出excel uses Dataset2Excel; procedure Tfdm.exportXLS(dataset: TDataSet);var xls: TData ...

  5. linux使用其它用户 su - op -c

    su - op -c "whoami"

  6. shell用法 (cat << EOF)

    下面的语句会创建不存在的secure.config,如果存在直接追加,然后把多行内容: [database]        password = gerrit 写入文件secure.config ca ...

  7. Less资源汇总

    GUI编译工具 为方便起见,建议初学者使用GUI编译工具来编译.less文件,以下是一些可选GUI编译工具: koala(Win/Mac/Linux) 国人开发的LESSCSS/SASS编译工具.下载 ...

  8. 顶点缓存对象(VBO)【转】

    http://www.cnblogs.com/hefee/p/3824300.html 顶点缓存对象(VBO) 创建VBO 绘制VBO 更新VBO 实例 GL_ARB_vertex_buffer_ob ...

  9. hdu1698 Just a Hook (线段树区间更新 懒惰标记)

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  10. mac 上多版本python 共存

    Mac上自带了Python2.x的版本,有时需要使用Python3.x版本做开发,但不能删了Python2.x,可能引起系统不稳定,那么就需要安装多个版本的Python. 1.安装Python3.x版 ...