【经典LeetCode算法题目专栏分类】【第1期】左右双指针系列:盛最多水的容器、接雨水、回文子串、三数之和

简介: 【经典LeetCode算法题目专栏分类】【第1期】左右双指针系列:盛最多水的容器、接雨水、回文子串、三数之和

1. 盛最多水的容器

class Solution:
    def maxArea(self, height: List[int]) -> int:
        # 左右双指针
        left = 0
        right = len(height) - 1
        res = 0
        while left < right:
            if height[left] > height[right]:
                area = (right - left) * height[right]
                right -= 1
            else:
                area = (right - left) * height[left]
                left += 1
            res = max(res, area)
        return res

2.接雨水

# 左右双指针
class Solution:
    def trap(self, height: List[int]) -> int:
        # 左右双指针,一般通过while left<right循环作为终止条件
        # 某一点处的雨水与左右最大柱子的高度有关
        # left_max与right_max用于动态存储左右从左向右,及从右向左的最大高度
        # 如果left_max < right_max ,则只用考虑left点处左侧的高度即可,
        # 因为右侧的最大高度肯定比左侧高,相当于在left点处取到了左侧最大值与右侧最大值中的较小者
        n = len(height)
        left = 0
        right = n - 1
        left_max = 0
        right_max = 0
        ans = 0
        while left < right:
            if height[left] < height[right]:
                left_max = max(left_max,height[left])
                ans += left_max - height[left]
                left += 1
            else:
                right_max = max(right_max,height[right])
                ans += right_max - height[right]
                right -= 1
        return ans
#单调递减栈
class Solution:
    def trap(self, height: List[int]) -> int:
        #单调递减栈
        res = 0
        st = []
        for i in range(len(height)):
            while st and height[st[-1]] < height[i]:
                cur = st.pop()
                if not st:
                    break
                l = st[-1]
                r = i
                h = min(height[r],height[l]) - height[cur]
                res += (r-l-1) * h
            st.append(i)
        return res

3.回文子串

class Solution:
    def countSubstrings(self, s: str) -> int:
        num = 0
        n = len(s)
        for i in range(n): # 遍历回文中心点
            for j in [0,1]: # j=0,中心是一个点,j=1,中心是两个点
                l = i
                r = i + j
                while l >= 0 and r < n and s[l] == s[r]:
                    num += 1
                    l -= 1
                    r += 1
        return num

4.三数之和

class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        # 通过排序 + 双指针实现
        nums.sort()
        res = []
        k = 0
        n = len(nums)
        for k in range(n - 2):
            if nums[k] > 0: 
                break # 1. because of j > i > k.
            if k > 0 and nums[k] == nums[k - 1]: 
                continue # 2. skip the same `nums[k]`.
            i, j = k + 1, n - 1
            while i < j: # 3. double pointer
                s = nums[k] + nums[i] + nums[j]
                if s < 0:
                    i += 1
                elif s > 0:
                    j -= 1
                else:
                    res.append([nums[k], nums[i], nums[j]])
                    i += 1
                    j -= 1
                    while i < j and nums[i] == nums[i - 1]: i += 1 # 跳过相同元素
                    while i < j and nums[j] == nums[j + 1]: j -= 1 # 跳过相同元素
        return res


相关文章
|
5月前
|
算法
Leetcode 初级算法 --- 数组篇
Leetcode 初级算法 --- 数组篇
66 0
|
3月前
|
算法 容器
【算法】——双指针算法合集(力扣)
移动零,复写零,快乐数,盛最多水的容器,有效三角形的个数,和为s的两个数(查找总价格为目标值的两个商品 ),三数之和,四数之和
|
4月前
|
存储 算法 Java
leetcode算法题-有效的括号(简单)
【11月更文挑战第5天】本文介绍了 LeetCode 上“有效的括号”这道题的解法。题目要求判断一个只包含括号字符的字符串是否有效。有效字符串需满足左括号必须用相同类型的右括号闭合,并且左括号必须以正确的顺序闭合。解题思路是使用栈数据结构,遍历字符串时将左括号压入栈中,遇到右括号时检查栈顶元素是否匹配。最后根据栈是否为空来判断字符串中的括号是否有效。示例代码包括 Python 和 Java 版本。
|
5月前
|
传感器 数据处理 定位技术
多线程;顺序容器;智能指针
【10月更文挑战第14天】多线程的创建创建线程比较简单,C++提供头文件thread,使用std的thread实例化一个线程对象创建。 std::thread 在 #include 头文件中声明,因此使用 std::thread 时需要包含 #include 头文件。
|
5月前
|
算法
每日一道算法题(Leetcode 20)
每日一道算法题(Leetcode 20)
56 2
|
5月前
|
容器
Leetcode第十一题(盛最多水的容器)
LeetCode第十一题要求找出两条线,使得它们与x轴构成的容器能盛最多的水,通常使用双指针法来解决,通过移动较短的一边来尝试增加容量。
29 0
Leetcode第十一题(盛最多水的容器)
|
5月前
|
传感器 数据处理 定位技术
多线程;顺序容器;智能指针
多线程的创建创建线程比较简单,C++提供头文件thread,使用std的thread实例化一个线程对象创建。 std::thread 在 #include 头文件中声明,因此使用 std::thread 时需要包含 #include 头文件。 #include &lt;iostream&gt; #include &lt;thread&gt; #include &lt;stdlib.h&gt; //sleep using namespace std; void t1() //普通的函数,用来执行线程 { for (int i = 0; i &lt; 10; ++i)
多线程;顺序容器;智能指针
|
7月前
|
算法
测试工程师的技能升级:LeetCode算法挑战与职业成长
这篇文章通过作者亲身体验LeetCode算法题的过程,探讨了测试工程师学习算法的重要性,并强调了算法技能对于测试职业成长的必要性。
112 1
测试工程师的技能升级:LeetCode算法挑战与职业成长
|
7月前
|
算法 容器
LeetCode第11题盛最多水的容器
该文章介绍了 LeetCode 第 11 题盛最多水的容器的解法,通过分析得出只能移动短板才可能使面积变大的规律,使用双指针法解决该问题,避免了穷举法的高时间复杂度,并总结了算法题需要多实践、思考和积累技巧来提升解题能力。
LeetCode第11题盛最多水的容器