DW_Leetcode编程实践_Day3

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

DW_Leetcode编程实践_Day3_第1张图片

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

DW_Leetcode编程实践_Day3_第2张图片

14.三数之和

题目链接:三数之和

太难了,先放弃。

你可能感兴趣的:(leetcode,python)