leetcode: Two Sum
2013-10-29 15:37
323 查看
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
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
思路
先将输入的序列排序,然后对每一个数字,使用二分法寻找目标和与这个数字的差,如果找到则返回。用桶排序貌似更快,但是总感觉这个不是一个根本上的提升,其他的方法和思路目前还不知道。下面是accepted的代码,排序使用的是堆排序class Solution{ public: vector<int> twoSum(vector<int> &numbers, int target){ vector<int> res; res.push_back(0); res.push_back(0); if(numbers.size()<2) return res; vector<pair<int,int> > index_numbers; for(int i=0;i<numbers.size();i++) { index_numbers.push_back(make_pair(numbers[i],i)); } heapSort(index_numbers); for(int i=0;i<index_numbers.size();i++) { int t=target-index_numbers[i].first; int ls=0,rs=index_numbers.size()-1; if(t>index_numbers[rs].first) continue; if(t<index_numbers[ls].first) continue; for(;ls<=rs;) { int ms=(ls+rs)/2; if(t<index_numbers[ms].first) rs=ms-1; else if(t>index_numbers[ms].first) ls=ms+1; else { if(ms==i) { if(ms>=1 && t==index_numbers[ms-1].first) { res[0]=index_numbers[ms-1].second+1; res[1]=index_numbers[i].second+1; if(res[0]>res[1]) swap(res[0],res[1]); return res; } if(ms<=index_numbers.size()-2 && t==index_numbers[ms+1].first) { res[0]=index_numbers[i].second+1; res[1]=index_numbers[ms+1].second+1; if(res[0]>res[1]) swap(res[0],res[1]); return res; } break; } res[0]=index_numbers[i].second+1; res[1]=index_numbers[ms].second+1; if(res[0]>res[1]) swap(res[0],res[1]); return res; } } } return res; } inline void swap(int &l,int &r) { int tmp=r;r=l;l=tmp; } void heapBuild(vector<pair<int,int> > &numbers) { for(int i=numbers.size()-1;i>=0;i--) { heapAdjust(numbers,i,numbers.size()-i); } } void heapSort(vector<pair<int,int> > &numbers) { heapBuild(numbers); for(int i=numbers.size()-1;i>0;i--) { pair<int,int> tmp=numbers[0]; numbers[0]=numbers[i]; numbers[i]=tmp; heapAdjust(numbers,0,i); } } void heapAdjust(vector<pair<int,int> > &numbers, int cur,int length) { for(int i=cur;i<cur+length;) { int left=leftChild(i); int right=rightChild(i); if(left>=cur+length) return; int biggerChild=left; if(left<cur+length-1 && smallerThan(numbers[left],numbers[right])) biggerChild=right; if(biggerThan(numbers[biggerChild],numbers[i])) { pair<int,int> tmp=numbers[i]; numbers[i]=numbers[biggerChild]; numbers[biggerChild]=tmp; } i=biggerChild; } } bool smallerThan(pair<int,int> l,pair<int,int> r) { return l.first<r.first; } bool biggerThan(pair<int,int> l,pair<int,int> r) { return l.first>r.first; } inline int leftChild(int i) { return 2*i+1; } inline int rightChild(int i) { return 2*i+2; } };
相关文章推荐
- 【Leetcode】Sum of Two Integers
- Sum of Two Integers(leetcode)
- LeetCode 1 : Two Sum ---- 哈希&数组
- LeetCode - Two Sum
- leetcode刷题,我的解法1 twosum问题
- Leetcode | Two Sum
- Leetcode|Two Sum III - Data structure design
- leetcode mock interview-two sum II
- (leetcode)1. 查找数组中两个数之和为给定值Two Sum---Java
- [Leetcode] 712. Minimum ASCII Delete Sum for Two Strings 解题报告
- LeetCode Two Sum
- Leetcode 1.Two Sum(java版)
- LeetCode(371) Sum of Two Integers
- 【leetcode】two-sum
- 2016.07.13-map的使用(以leetcode1-Two Sum为例)
- [LeetCode] Minimum ASCII Delete Sum for Two Strings 两个字符串的最小ASCII删除和
- [leetcode笔记] Two-sum
- Two Sum leetcode
- leetcode--sum of two integers
- leetcode — two-sum