【经典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月前
|
存储 算法
算法入门:专题二---滑动窗口(长度最小的子数组)类型题目攻克!
给定一个正整数数组和目标值target,找出总和大于等于target的最短连续子数组长度。利用滑动窗口(双指针)优化,维护窗口内元素和,通过单调性避免重复枚举,时间复杂度O(n)。当窗口和满足条件时收缩左边界,更新最小长度,最终返回结果。
|
5月前
|
存储 算法 编译器
算法入门:剑指offer改编题目:查找总价格为目标值的两个商品
给定递增数组和目标值target,找出两数之和等于target的两个数字。利用双指针法,left从头、right从尾向中间逼近,根据和与target的大小关系调整指针,时间复杂度O(n),空间复杂度O(1)。找不到时返回{-1,-1}。
|
传感器 数据处理 定位技术
多线程;顺序容器;智能指针
【10月更文挑战第14天】多线程的创建创建线程比较简单,C++提供头文件thread,使用std的thread实例化一个线程对象创建。 std::thread 在 #include 头文件中声明,因此使用 std::thread 时需要包含 #include 头文件。
118 1
|
传感器 数据处理 定位技术
多线程;顺序容器;智能指针
多线程的创建创建线程比较简单,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)
多线程;顺序容器;智能指针
|
算法 Java
LeetCode经典算法题:矩阵中省份数量经典题目+三角形最大周长java多种解法详解
LeetCode经典算法题:矩阵中省份数量经典题目+三角形最大周长java多种解法详解
217 6
|
存储 编译器 C语言
【C语言】指针练习题目
【C语言】指针练习题目
354 2
|
传感器 数据处理 定位技术
多线程;顺序容器;智能指针
多线程的创建创建线程比较简单,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)
106 0
|
安全 编译器 容器
C++STL容器和智能指针
C++STL容器和智能指针
|
存储 C语言
C语言如何使用结构体和指针来操作动态分配的内存
在C语言中,通过定义结构体并使用指向该结构体的指针,可以对动态分配的内存进行操作。首先利用 `malloc` 或 `calloc` 分配内存,然后通过指针访问和修改结构体成员,最后用 `free` 释放内存,实现资源的有效管理。
1600 13