【LeetCode】167. Two Sum II - Input array is sorted
2017-08-25 14:07
537 查看
Given an array of integers that is already sorted in ascending order, 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 and you may not use the same element twice.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
方法一使用二分查找,时间复杂度为nlogn
方法2:使用头尾两个指针,一次遍历,0(N)时间复杂度即可。
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 and you may not use the same element twice.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
方法一使用二分查找,时间复杂度为nlogn
class Solution { public: int find(vector<int>& a,int num){ int l=0,r=a.size()-1; int mid=(l+r)/2; while(l<=r){ mid=(l+r)/2; if(a[mid]==num){ return mid; }else if(a[mid]>num){ r=mid-1; }else{ l=mid+1; } } return -1; } vector<int> twoSum(vector<int>& numbers, int target) { vector<int> ans; for(int i=0;i<numbers.size();i++){ int index=find(numbers,target-numbers[i]); if(index!=-1&&index!=i){ ans.push_back(i+1); ans.push_back(index+1); break; } } if(ans[0]>ans[1])swap(ans[0],ans[1]); return ans; } };
方法2:使用头尾两个指针,一次遍历,0(N)时间复杂度即可。
class Solution { public: vector<int> twoSum(vector<int>& numbers, int target) { vector<int> ans; int i=0,j=numbers.size()-1; while(i<j){ int sum=numbers[i]+numbers[j]; if(sum==target){ ans.push_back(i+1); ans.push_back(j+1); break; }else if(sum<target){ i++; }else{ j--; } } return ans; } };
相关文章推荐
- Leetcode167. Two Sum II - Input array is sorted
- LeetCode 167. Two Sum II - Input array is sorted
- [LeetCode] 167. Two Sum II - Input array is sorted
- LeetCode 167. Two Sum II - Input array is sorted
- LeetCode: 167. Two Sum II - Input array is sorted
- LeetCode 167. Two Sum II - Input array is sorted 题解(C++)
- LeetCode-Easy部分中标签为 Binary Search 167. Two Sum II - Input array is sorted
- LeetCode-167. Two Sum II - Input array is sorted
- LeetCode 167. Two Sum II - Input array is sorted
- [leetcode] 1. twoSum; 167. Two Sum II - Input array is sorted; 15. threeSum; 16. 3Sum Closest; 18. 4
- Leetcode:167. Two Sum II - Input array is sorted 求数组中的两个数的和等于给定值,求这两个数的下标
- [勇者闯LeetCode] 167. Two Sum II - Input array is sorted
- LeetCode 167. Two Sum II - Input array is sorted (双索引技术对撞指针)
- 【Leetcode】167. Two Sum II - Input array is sorted
- LeetCode - 167. Two Sum II - Input array is sorted
- 【LeetCode】167. Two Sum II - Input array is sorted
- leetcode 167. Two Sum II - Input array is sorted
- 【Leetcode】167. Two Sum II - Input array is sorted
- Leetcode 167. Two Sum II - Input array is sorted
- leetcode题解-167. Two Sum II - Input array is sorted