56. Two Sum【easy】
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are zero-based.
Notice
You may assume that each input would have exactly one solution
numbers=[2, 7, 11, 15], target=9
return [0, 1]
Either of the following solutions are acceptable:
- O(n) Space, O(nlogn) Time
- O(n) Space, O(n) Time
题意
给一个整数数组,找到两个数使得他们的和等于一个给定的数 target。
你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标。注意这里下标的范围是 0 到 n-1。
解法一:
class Solution {
public:
/*
* @param numbers: An array of Integer
* @param target: target = numbers[index1] + numbers[index2]
* @return: [index1 + 1, index2 + 1] (index1 < index2)
*/
vector<int> twoSum(vector<int>& nums, int target) {
// hash[i]表示nums中数值为i的下标
unordered_map<int, int> hash;
vector<int> result;
// 一边循环每个数,一边加入hash表。
for (int i = ; i < nums.size(); i++) {
if (hash.find(target - nums[i]) != hash.end()) {
// target - nums[i]的下标更小,放在前面
result.push_back(hash[target - nums[i]]);
result.push_back(i);
return result;
}
hash[nums[i]] = i;
}
// 无解的情况
result.push_back(-);
result.push_back(-);
return result;
}
};
使用万能的hash,参考@NineChapter 的代码
解法二:
class Solution {
public:
/*
* @param numbers: An array of Integer
* @param target: target = numbers[index1] + numbers[index2]
* @return: [index1 + 1, index2 + 1] (index1 < index2)
*/
vector<int> twoSum(vector<int> &numbers, int target) {
int len = numbers.size();
int i, j;
int flag = ;//flag作为找到答案后跳出的一个标记用变量
for (i = ; i < len; ++i) {
for (j = i + ; j < len; ++j) {
if (numbers[i] + numbers[j] == target) {
flag=;
break;
}
}
if (flag)
break;
}
vector<int> ans;
ans.push_back(i);
ans.push_back(j);
return ans;
}
};
暴力破解法,完全不推荐
56. Two Sum【easy】的更多相关文章
- 1. Two Sum【easy】
1. Two Sum[easy] Given an array of integers, return indices of the two numbers such that they add up ...
- 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 ...
随机推荐
- Latex中为作者添加多个单位属性(IEEE模板)
\author{ \IEEEauthorblockN{name1 name1\IEEEauthorrefmark{1}\IEEEauthorrefmark{2}, name2 name2\IEEEa ...
- calibre怎么转换文件格式
首先打开calibre软件,需要转换的书目不在列表的按照上一个教程将文件导入书籍列表. 选中需要转化格式的书籍之后右击,会跳出一个比较长的菜单栏,找到转换书籍选项,此处有两个选项,一个是逐个转换,另外 ...
- vnstat 查看服务器带宽统计命令
vnStat是一个Linux下的网络流量监控软件,它记录指定网卡每日的传输流量日志. 它并非基于网络包的过滤,而是分析文件系统- /proc, 所以vnStat无需root的权限就可使用. ,它还自带 ...
- Cocos2d-X中的动作特效
Cocos2d-X中提供了很丰富的动作特效 比如:网格动画 扭曲特效 3D瓷砖波动特效 程序代码: #include "ActionEffect.h" #include " ...
- 【python】Django设置SESSION超时时间没有生效?
按手册和网上的方法在settings.py中设置“SESSION_COOKIE_AGE” 和 “SESSION_EXPIRE_AT_BROWSER_CLOSE” 均不生效. 通过查看django的源代 ...
- ES6 中 Symbol.split的用法
class Split1 { constructor(value) { this.value = value; } [Symbol.split](string) { var index = strin ...
- Ubuntu下压缩包内文件解压后乱码问题的解决
用到的工具是The Unarchiver项目提供的lsar/unar工具. The Unarchiver项目主页:http://code.google.com/p/theunarchiver/ 安装( ...
- 使用ionic中的侧边栏以及angularjs中广播的使用
接着之前的ionic的例子 查看例子:我的第一段ionic代码 demo3.html(黄底内容为增加或修改的内容) <!DOCTYPE html> <html ng-app=&quo ...
- ES PS TS 流的区别
http://fengqing888.blog.163.com/blog/static/330114162012111805717584/ ES是原始码流,包含视频.音频或数据的连续码流.TS是传输流 ...
- 扩展MSEG 加入Z字段
append MSEG append IMSEG append BAPI_TE_XMSEG 实现 BADI: MB_BAPI_GOODSMVT_CREATE BAPI中: BAPI_ ...