11.呈最多水的容器
题目链接:呈最多水的容器
题目理解:选取两个指针,指针开始位置为第一个柱子和最后一个柱子。取矮的一个计算面积,计算之后的指针位置就要从矮的位置往中间走一下。直到两个指针碰到。
class Solution:
def maxArea(self, height: List[int]) -> int:
i, j, res = 0, len(height) - 1, 0
while i < j:
if height[i] < height[j]:
res = max(res, height[i] * (j - i))
i += 1
else:
res = max(res, height[j] * (j - i))
j -= 1
return res

14.最长公共前缀
题目链接:最长公共前缀
题目理解:先将列表中的单词排序,然后看第一个单词和最后一个单词的共同前缀就可以了。
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if not strs:
return ""
strs.sort()
n = len(strs)
a = strs[0]
b = strs[n-1]
res = ""
for i in range(len(a)):
if i < len(b) and a[i] == b[i]:
res += a[i]
else:
break
return res

14.三数之和
题目链接:三数之和
太难了,先放弃。
博客围绕LeetCode三道题展开。“呈最多水的容器”题,用双指针法,从首尾开始,取矮柱算面积并向中间移动指针;“最长公共前缀”题,先对列表单词排序,再找首尾单词的共同前缀;“三数之和”题因难度大暂时放弃。

被折叠的 条评论
为什么被折叠?



