腾讯精选50题—Day3题目11,14,15

腾讯精选50题—Day3题目11,14,15

打卡的第三天,忙碌而充实,用一句话来鼓励自己~

The measure of success is how we cope with disappointment.

目录

腾讯精选50题—Day3题目11,14,15

1. 题目11 盛水最多的容器

(1)题目描述

2. 题目14 最长公共前缀

(1)题目描述

(2)思路

(3)题解

3. 题目15 三数之和

(1)题目描述

(2)思路

(3)题解

 参考


1. 题目11 盛水最多的容器

(1)题目描述

Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of the line i is at (i, ai) and (i, 0). Find two lines, which, together with the x-axis forms a container, such that the container contains the most water.

Notice that you may not slant the container.

示例:

 约束:

(2)思路

这是一道经典的双指针移动题目,需要注意的是容器的盛水量取决于最矮的那一边,设置两个指针low指向头,high指向尾,用Max_A保存最大盛水量,比较low和high指向的位置的元素大小,然后移动较小元素所指的指针(low++或者high--)。

(3)题解

class Solution {
public:
    int maxArea(vector<int>& number) {

        int len = number.size();
        int low = 0;
        int high = len - 1;
        int Max_A = 0;

        while (low != high)
        {
            int hei = min(number[low], number[high]);
            int wid = high - low;

            int temp_area = hei * wid;

            if (temp_area > Max_A)
            {
                Max_A = temp_area;
            }

            if (number[low] <= number[high])
                low++;
            else
                high--;
        }
        return Max_A;
    }
};

结果:

时间复杂度:O(n)

空间复杂度:O(1) 

2. 题目14 最长公共前缀

(1)题目描述

(2)思路

借用双指针法思想,定义low和high两个指针,每次求low和high的最长公共前缀,然后和公共前缀比较,较小的部分作为新的公共前缀,如果公共前缀和利用low和high求出的最长公共前缀无重叠,那么直接返回。

(3)题解

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {

        int len = strs.size();
        string result = "";
        int low = 0;
        int high = len - 1;
        string prefix = "";
        bool flag = false;

        if (len < 1) return result;
        if (len == 1)
        {
            result = strs[0];
            return result;
        }

        while (low != high)
        {
            string temp_profix = findProfix(strs[low], strs[high]);

            if (!flag)
            {
                prefix = temp_profix;
                flag = true;
            }
            else {
                int sublen = min(temp_profix.size(), prefix.size());

                for (int st_k = 0; st_k < sublen; st_k++)
                {
                    if (temp_profix[st_k] != prefix[st_k])
                    {
                        return "";
                    }
                }
                if (temp_profix.size() < prefix.size())
                    prefix = temp_profix;
            }

            if (strs[low].size() <= strs[high].size())
                high--;
            else {
                low++;
            }

        }
        return prefix;
    }

    string findProfix(string str_a, string str_b)
    {
        int len = min(str_a.size(), str_b.size());
        string find_result = "";

        for (int i = 0; i < len; i++)
        {
            if (str_a[i] == str_b[i])
            {
                find_result += str_a[i];
            }
            else {
                break;
            }
        }

        return find_result;
    }
};

时间复杂度:O(s),s为所有字符串的长度之和

空间复杂度:O(1) 

结果:

3. 题目15 三数之和

(1)题目描述

(2)思路

这道题目是经典的双指针法,双指针法需要先将数组排序,然后特别需要注意的是如何去重,可以在遍历的时候巧妙去重。

(3)题解

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {

        sort(nums.begin(), nums.end());
        vector<vector<int>> result;

        int len = nums.size();

        if (len < 3 || nums[0] > 0)
            return result;

        int low = 0;
        int high = 0;

        for (int i = 0; i < len; i++)
        {
            if (i > 0 && nums[i] == nums[i - 1])
                continue;
            low = i + 1;
            high = len - 1;//

            while (low < high)
            {

                int temp_i = nums[i];
                int temp_low = nums[low];
                int temp_high = nums[high];
                int temp_add = temp_i + temp_low + temp_high;

                if (temp_add == 0)
                {
                    vector<int> temp;
                    temp.clear();
                    temp.push_back(temp_i);
                    temp.push_back(temp_low);
                    temp.push_back(temp_high);
                    result.push_back(temp);

                    while (low < high && nums[high] == nums[high - 1])
                        high--;
                    while (low < high && nums[low] == nums[low + 1])
                        low++;

                    high--;
                    low++;

                }
                else if (temp_add < 0)
                {
                    low++;
                }
                else {
                    high--;
                }
            }
        }

        return result;
    }
};

时间复杂度:O(n^{2})

空间复杂度:O(1) 

结果:

 参考

1. https://leetcode-cn.com/problems/3sum/solution/15-san-shu-zhi-he-ha-xi-fa-shuang-zhi-zhen-fa-xi-2/

猜你喜欢

转载自blog.csdn.net/Fox_Alex/article/details/112554693