【LeetCode】Two pointers to solve two numbers whose sum is s

insert image description here

Problem: Sword refers to Offer 57. Two numbers whose sum is s

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 numssearch in this array. If the sum of the two numbers is found target, then form a result set and return

1.jpg

Algorithm thinking analysis

Next, let's analyze the idea of ​​this question

  1. 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)

2.jpg

  1. 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 leftpointer points to the leftmost end, and one rightpointer points to the rightmost end. Through back and forth traversal, we can find which two numbers can form the result [target]

3.jpg

  • 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++

4.jpg

  • Next, let's consider this nums[left] + nums[right] > targetsituation 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 isright--

5.jpg

  • If the last one is found, we only need to return the result set of these two data.

6.jpg

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};
    }
};

insert image description here

Guess you like

Origin blog.csdn.net/Fire_Cloud_1/article/details/132699366