Two Sum
2015-07-31 14:56
246 查看
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 not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
题意是从一堆无序的数中,找到两个数,使其和等于给定值。分析可以知道,有几种方法,既然是无序的,可以考虑先排序,然后用双指针从头到尾移动,知道找到合适的两个数,这要注意对下标的保存,因为题目要求返回的是下标;
第二种方法,不用排序,对于给定的值i,找数组中是否存在另外一个值target - i,存在则返回这两个值的下标,这要借助于hashTable来O(1)的查找。
第三种方法就是,直接暴力,枚举所有的两个数的和的可能性,这样时间复杂度很高,达到了O(N^2)
直接查找:
class Solution {
public:
//直接查找
vector<int> twoSum(vector<int>& nums, int target) {
unsigned numLen = nums.size();
vector<int> ans;
if(numLen > 1)
{
unordered_map<int, set<int>> hmap;
for(unsigned i = 0; i < numLen; ++i)
{
hmap[nums[i]].insert(i + 1);
}
int tmpTarget;
for(unsigned i = 0; i < numLen; ++i)
{
tmpTarget = target - nums[i];
if(tmpTarget == nums[i] && hmap[tmpTarget].size() >= 2)
{
ans.push_back(*(hmap[nums[i]].begin()));
hmap[nums[i]].erase(hmap[nums[i]].begin());
ans.push_back(*(hmap[nums[i]].begin()));
return ans;
}
else if(tmpTarget != nums[i] && hmap.find(tmpTarget) != hmap.end())
{
ans.push_back(i + 1);
ans.push_back(*(hmap[tmpTarget].begin()));
return ans;
}
}
}
return ans;
}
};
排序+双指针
typedef struct Node
{
int val;
int index;
Node(int v, int idx) : val(v), index(idx) {}
}Node;
bool comp(const Node &a, const Node &b)
{
if(a.val == b.val)
return a.index < b.index;
return a.val < b.val;
}
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int numLen = nums.size();
vector<int> ans;
if(numLen > 1)
{
vector<Node> NodeVec;
for(unsigned i = 0; i < numLen; ++i)
{
NodeVec.push_back(Node(nums[i], i + 1));
}
sort(NodeVec.begin(), NodeVec.end(), comp);
int low = 0, high = numLen - 1, tmpSum = 0;
while(low < high)
{
tmpSum = NodeVec[low].val + NodeVec[high].val;
if(tmpSum == target)
{
ans.push_back((NodeVec[low].index < NodeVec[high].index ? NodeVec[low].index : NodeVec[high].index));
ans.push_back((NodeVec[low].index > NodeVec[high].index ? NodeVec[low].index : NodeVec[high].index));
return ans;
}
else if(tmpSum < target)
{
++low;
}
else
--high;
}
}
return ans;
}
};
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 not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
题意是从一堆无序的数中,找到两个数,使其和等于给定值。分析可以知道,有几种方法,既然是无序的,可以考虑先排序,然后用双指针从头到尾移动,知道找到合适的两个数,这要注意对下标的保存,因为题目要求返回的是下标;
第二种方法,不用排序,对于给定的值i,找数组中是否存在另外一个值target - i,存在则返回这两个值的下标,这要借助于hashTable来O(1)的查找。
第三种方法就是,直接暴力,枚举所有的两个数的和的可能性,这样时间复杂度很高,达到了O(N^2)
直接查找:
class Solution {
public:
//直接查找
vector<int> twoSum(vector<int>& nums, int target) {
unsigned numLen = nums.size();
vector<int> ans;
if(numLen > 1)
{
unordered_map<int, set<int>> hmap;
for(unsigned i = 0; i < numLen; ++i)
{
hmap[nums[i]].insert(i + 1);
}
int tmpTarget;
for(unsigned i = 0; i < numLen; ++i)
{
tmpTarget = target - nums[i];
if(tmpTarget == nums[i] && hmap[tmpTarget].size() >= 2)
{
ans.push_back(*(hmap[nums[i]].begin()));
hmap[nums[i]].erase(hmap[nums[i]].begin());
ans.push_back(*(hmap[nums[i]].begin()));
return ans;
}
else if(tmpTarget != nums[i] && hmap.find(tmpTarget) != hmap.end())
{
ans.push_back(i + 1);
ans.push_back(*(hmap[tmpTarget].begin()));
return ans;
}
}
}
return ans;
}
};
排序+双指针
typedef struct Node
{
int val;
int index;
Node(int v, int idx) : val(v), index(idx) {}
}Node;
bool comp(const Node &a, const Node &b)
{
if(a.val == b.val)
return a.index < b.index;
return a.val < b.val;
}
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int numLen = nums.size();
vector<int> ans;
if(numLen > 1)
{
vector<Node> NodeVec;
for(unsigned i = 0; i < numLen; ++i)
{
NodeVec.push_back(Node(nums[i], i + 1));
}
sort(NodeVec.begin(), NodeVec.end(), comp);
int low = 0, high = numLen - 1, tmpSum = 0;
while(low < high)
{
tmpSum = NodeVec[low].val + NodeVec[high].val;
if(tmpSum == target)
{
ans.push_back((NodeVec[low].index < NodeVec[high].index ? NodeVec[low].index : NodeVec[high].index));
ans.push_back((NodeVec[low].index > NodeVec[high].index ? NodeVec[low].index : NodeVec[high].index));
return ans;
}
else if(tmpSum < target)
{
++low;
}
else
--high;
}
}
return ans;
}
};
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解
- LeetCode [Day 5] Longest Palindromic Substring 题解
- LeetCode [Day 6] ZigZag Conversion 题解