[Preferred algorithm question practice] day2


1. 11. The container that holds the most water

1. Introduction to the topic

11. The container that holds the most water.
Given an integer array height of length n. There are n vertical lines, and the two endpoints of the i-th line are (i, 0) and (i, height[i]).
Find the two lines such that the container they form with the x-axis can hold the most water.
Returns the maximum amount of water the container can store.
Note: You cannot tilt the container.
Insert image description here
Insert image description here

2. Problem-solving ideas

3.Code

class Solution {
    
    
public:
    int maxArea(vector<int>& height) {
    
    
        int left = 0, right = height.size() - 1;
        int ret = 0;
        while(left < right)
        {
    
    
            if(height[left] < height[right])
            {
    
    
                ret = max(ret, (right - left) * height[left]);
                left++;
            }
            else
            {
    
    
                ret = max(ret, (right - left) * height[right]);
                right--;
            }
        }
        return ret;
    }
};

4. Running results

Insert image description here

2. 611. Number of valid triangles

1. Introduction to the topic

611. Number of valid triangles
Given an array nums containing non-negative integers, return the number of triples that can form three sides of a triangle.
Insert image description here

2. Problem-solving ideas

3.Code

class Solution {
    
    
public:
    int triangleNumber(vector<int>& nums) {
    
    
        int ret = 0;
        //1.排序
        sort(nums.begin(), nums.end(), [](int& x, int& y){
    
    return x < y;});
        //2.确定三条边中最长的那条边,然后用双指针法遍历其他两条边
        for(int i = nums.size() - 1;i >= 2; --i)
        {
    
    
            int left = 0, right = i - 1;
            while(left < right)
            {
    
    
                if(nums[left] + nums[right] <= nums[i])//不能组成三角形
                {
    
    
                    left++;
                }
                else//可以组成三角形
                {
    
    
                    ret += right - left;//这个right的值和i的值可以组成的三角形的种数
                    right--;
                }
            }
        }
        return ret;
    }
};

4. Running results

Insert image description here

3. The sword points to Offer 57. Two numbers whose sum is s

1. Introduction to the topic

Sword Points to Offer 57. Two numbers whose sum is s
. Input an ascending sorted array and a number s. Find two numbers in the array such that their sum is exactly s. If the sum of multiple pairs of numbers is equal to s, just output any pair.
Insert image description here

2. Problem-solving ideas

3.Code

class Solution {
    
    
public:
    vector<int> twoSum(vector<int>& nums, int target) {
    
    
        //1.排序
        sort(nums.begin(), nums.end(), [](int& x, int& y){
    
    return x < y;});
        //2.用碰撞指针的方式找出符合条件的两个数
        int left = 0, right = nums.size() - 1;
        while(left < right)
        {
    
    
            int sum = nums[left] + nums[right];
            if(sum < target)
            {
    
    
                left++;
            }
            else if(sum > target)
            {
    
    
                right--;
            }
            else
            {
    
    
                return {
    
    nums[left], nums[right]};
            }
        }
        return {
    
    };
    }
};

4. Running results

Insert image description here


Summarize

Today is the second day of algorithm practice.
A journey of a thousand miles begins with a single step , keep going.
Source of the question: LeetCode, the copyright belongs to LeetCode.
If this article has inspired you, I hope you can support the author more, thank you all!

おすすめ

転載: blog.csdn.net/xjjxjy_2021/article/details/131556994