Problem: Sword refers to Offer 57. Two numbers whose sum is s
Article Directory
topic analysis
First, let's explain the idea of this topic
- We see that the meaning of this question is very simple. It is to
nums
search in this array. If the sum of the two numbers is foundtarget
, then form a result set and return
Algorithm thinking analysis
Next, let's analyze the idea of this question
- Brute force solution
- First of all, the first one, what we all think of is [violent solution], that is to use two layers of for loops to do the matching work one by one, but we can imagine that this kind of solution will definitely time out, so I won’t do it here much narrative
for(int i = 0;i < nums.size(); ++i)
for(int j = i + 1;j < nums.size(); ++j)
- Take advantage of monotonicity, use double pointer algorithm to solve
- In our case, we mainly use the solution of double pointers to solve this problem. One
left
pointer points to the leftmost end, and oneright
pointer points to the rightmost end. Through back and forth traversal, we can find which two numbers can form the result [target]
- There will be three situations here, the first is the first one, if we encounter it
nums[left] + nums[right] < target
, then we can do a tricky job at this time - Readers can read the meaning of this question carefully again, and then they can know that the array sequence is in increasing order, then since [2] and the largest [21] are smaller than the one before [21
target
] A few numbers will be even smaller, and we don't need to make multiple comparisons in one fell swoop at this time. At this point just executeleft++
- Next, let's consider this
nums[left] + nums[right] > target
situation again, then we know that this situation is also inconsistent, but we can make further simplifications when making judgments - Readers can think about it first, which number should we exclude at this time? That is obviously this [21], why? Think about this [21] and the smallest [11] will be exceeded
target
, then the rest of [15] and [19] will be exceeded even more? - So this [21] we should discard it, and the corresponding code is
right--
- If the last one is found, we only need to return the result set of these two data.
the complexity
- time complexity:
Considering the worst case, that is, when we traverse the search, the two left and right pointers meet, that is, it is not found, that is to traverse the sequence once, and its time complexity is O ( n ) O(n )O ( n )
- Space complexity:
No additional space is allocated, so the space complexity is O ( 1 ) O(1)O(1)
Code
The following is the code display
- Let’s talk about this and this
{nums[left], nums[right]}
, some friends may not be very clear, this belongs to the relevant knowledge of C++ initialization list - Generally, when we return a collection of two numbers in LeetCode, we don’t need to create a new vector collection, but will form a vector collection through implicit type conversion to return
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int left = 0, right = nums.size() - 1;
int sum = 0;
while(left < right)
{
sum = 0;
sum = nums[left] + nums[right];
if(sum < target){
left++;
}else if(sum > target){
right--;
}else{
return {
nums[left], nums[right]};
}
}
return {
-1, -1};
}
};