9、10、10回文数 正则 和容器

果然一周一打卡的结果就是已经忘了自己当初的思路。

9  回文数   好像很简单 我、反转看看反转后等不等于原来的数 可是为啥空间还是被7成的人打败。但是时间只有百分7

class Solution(object):
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        if x < 0:
            return False
        reverse = 0
        temp = x
        while(temp>0):
            reverse = reverse*10 + temp%10
            temp = temp//10
        if reverse == x :
            return True
        return False

10:

别问我 抄答案的

class Solution(object):
    def isMatch(self, s, p):
        """
        :type s: str
        :type p: str
        :rtype: bool
        """
        m, n = len(s), len(p)

        def matches(i, j):
            if i == 0:
                return False
            if p[j - 1] == '.':
                return True
            return s[i - 1] == p[j - 1]

        f = [[False] * (n + 1) for _ in range(m + 1)]
        f[0][0] = True
        for i in range(m + 1):
            for j in range(1, n + 1):
                if p[j - 1] == '*':
                    f[i][j] |= f[i][j - 2]
                    if matches(i, j - 1):
                        f[i][j] |= f[i - 1][j]
                else:
                    if matches(i, j):
                        f[i][j] |= f[i - 1][j - 1]
        return f[m][n]

 

 

11:容器  核心是找边界

 

class Solution(object):
    def maxArea(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        l = 0
        r = len(height)-1
        max = 0
        while l < r:
            area = min(height[l],height[r])*(r-l)
            if area > max :
                max = area
            if height[l]>height[r]:
                r -= 1
            else:
                l += 1
        return max

作者:yizhu-jia
链接:https://leetcode-cn.com/problems/container-with-most-water/solution/ai-zi-ji-zuan-lei-niu-jiao-jian-by-yizhu-g5xm/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

posted @ 2021-08-15 18:15  yi术家  阅读(43)  评论(0)    收藏  举报