LeetCode 笔记七 回文整数判断以及正则匹配

Palindrome Number

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example

Example 1

Input: 121
Output: true

Example 2

Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3

Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Code

这个题目也是很简单那种,就直接贴代码吧:

class Solution:
    def isPalindrome(self, x: int) -> bool:
        temp = str(x)
        if temp == temp[:][::-1]:
            return True
        else:
            return False

结果:

11509 / 11509 test cases passed.
Runtime: 56 ms
Memory Usage: 13.8 MB

Runtime: 56 ms, faster than 97.11% of Python3 online submissions for Palindrome Number.
Memory Usage: 13.8 MB, less than 6.50% of Python3 online submissions for Palindrome Number.

这个太简单了,明天再放新的在这里。

Regular Expression Matching (2019/09/30)

Given an input string (s) and a pattern §, implement regular expression matching with support for'.' and '*'.
'.' Matches any single character.
'*'Matches zero or more of the preceding element.
The matching should cover the entire input string (not partial).

Note:
s could be empty and contains only lowercase letters a-z.
p could be empty and contains only lowercase letters a-z, and characters like . or *.

Example

example 1

Input:
s = “aa”
p = “a”
Output: false
Explanation: “a” does not match the entire string “aa”.

example 2

Input:
s = “aa”
p = “a*”
Output: true
Explanation: ‘*’ means zero or more of the preceding element, ‘a’. Therefore, by repeating ‘a’ once, it becomes “aa”.

example 3

Input:
s = “ab”
p = “."
Output: true
Explanation: ".
” means “zero or more (*) of any character (.)”.

example 4

Input:
s = “aab”
p = “cab”
Output: true
Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore, it matches “aab”.

example 5

Input:
s = “mississippi”
p = “misisp*.”
Output: false

Code

就是调用自身函数这种写法看起来容易懂一些,如果不这样做,暂时只想到遍历,但这样子看起来也不是很好看hhh,就这样吧:

class Solution:
    def isMatch(self, s: str, p: str) -> bool:
        if p == s:
            return True
        elif ('.' not in p) and ('*' not in p):
            return False
        
        first_match = bool(s) and p[0] in {s[0], '.'}
        
        if len(p) >= 2 and p[1] == '*':
            return (self.isMatch(s, p[2:])) or (first_match and self.isMatch(s[1:], p))
        return first_match and self.isMatch(s[1:], p[1:])
        

明天就国庆了,今晚去花城广场那片溜达了一下,发现外面的气氛真浓郁呀,于是毅然决然决定明天继续实验室搬砖的一天!国庆快乐呀!冲鸭~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值