Datawhale编程实践(LeetCode 腾讯精选练习50)Task3

1.盛最多水的容器https://leetcode-cn.com/problems/container-with-most-water/

看了一会这道题只能想到暴力法,显然暴力法时间复杂度O(n^2),效率很低

看了官方解答,答案关键点为双指针https://leetcode-cn.com/problems/container-with-most-water/solution/sheng-zui-duo-shui-de-rong-qi-by-leetcode-solution/

 1 class Solution:
 2     def maxArea(self, height: List[int]) -> int:
 3         l, r = 0, len(height) - 1
 4         ans = 0
 5         while l < r:
 6             area = min(height[l], height[r]) * (r - l)
 7             ans = max(ans, area)
 8             if height[l] <= height[r]:
 9                 l += 1
10             else:
11                 r -= 1
12         return ans

双指针思想也是十分的巧妙,直接把时间复杂度缩小为O(n),空间复杂度也为O(1)

2.最长公共前缀https://leetcode-cn.com/problems/longest-common-prefix/

3.三数之和https://leetcode-cn.com/problems/3sum/

原文地址:https://www.cnblogs.com/zmbreathing/p/datawhale_leetcode_task3.html