【leetcode】1. Two Sum
2016-07-05 23:35
337 查看
一、题目描述
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.
Example:
UPDATE (2016/2/13):
The return format had been changed to zero-based indices. Please read the above updated description carefully.
题目解读:给一个整数序列和一个目标值,找出序列中两个数加起来等于目标值,返回这两个数在序列中的下标
思路:一开始只能想到写两个for循环遍历
c++代码(758ms,11.48%) 就这样还能排到11%呢 = = ! 汗....
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> result;
int left,right;
for(left=0; left<nums.size(); left++){
for(right = left+1; right<nums.size(); right++){
if((nums[left]+nums[right]) == target){
result.push_back(left);
result.push_back(right);
return result;
}//if
}//for
}//for
return result;
}
};
看讨论区别人的代码,在遍历的同时使用map存储已遍历过的数的位置,以便于快速查找,时间复杂度可以达到o(n)
c++代码(16ms,62.06%)
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
//Key is the number and value is its index in the vector.
unordered_map<int, int> hash;
vector<int> result;
for (int i = 0; i < numbers.size(); i++) {
int numberToFind = target - numbers[i];
//if numberToFind is found in map, return them
if (hash.find(numberToFind) != hash.end()) {
//+1 because indices are NOT zero based
result.push_back(hash[numberToFind]);
result.push_back(i);
return result;
}
//number was not found. Put it in the map.
hash[numbers[i]] = i;
}
return result;
}
};
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.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
UPDATE (2016/2/13):
The return format had been changed to zero-based indices. Please read the above updated description carefully.
题目解读:给一个整数序列和一个目标值,找出序列中两个数加起来等于目标值,返回这两个数在序列中的下标
思路:一开始只能想到写两个for循环遍历
c++代码(758ms,11.48%) 就这样还能排到11%呢 = = ! 汗....
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> result;
int left,right;
for(left=0; left<nums.size(); left++){
for(right = left+1; right<nums.size(); right++){
if((nums[left]+nums[right]) == target){
result.push_back(left);
result.push_back(right);
return result;
}//if
}//for
}//for
return result;
}
};
看讨论区别人的代码,在遍历的同时使用map存储已遍历过的数的位置,以便于快速查找,时间复杂度可以达到o(n)
c++代码(16ms,62.06%)
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
//Key is the number and value is its index in the vector.
unordered_map<int, int> hash;
vector<int> result;
for (int i = 0; i < numbers.size(); i++) {
int numberToFind = target - numbers[i];
//if numberToFind is found in map, return them
if (hash.find(numberToFind) != hash.end()) {
//+1 because indices are NOT zero based
result.push_back(hash[numberToFind]);
result.push_back(i);
return result;
}
//number was not found. Put it in the map.
hash[numbers[i]] = i;
}
return result;
}
};
相关文章推荐
- SQL Server 2014数据库远程访问设置方法
- javascript基础:DOM4操作表格和样式
- C++缺省参数
- windbg命令
- SimpleAdapter
- 大话设计模式-第28章 男人和女人--访问者模式
- 跟Google 学代码:Web Apps以及WebView究极优化
- HDU2029 Palindromes _easy version
- 高并发的核心技术-幂等的实现方案
- Linux信号、信号处理和信号处理函数
- Android 性能优化、内存优化(2) 工具使用
- POJ 3176 递推
- 硬币问题
- Magento-找出没有图片的产品
- 算法系列之一
- oracle sql 优化
- JavaSE基础-String相关
- Linux高并发服务器解决方案
- 游戏编程模式:命令模式(Part II)
- C++多态