LeetCode 笔记七 2019/09/29
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:])
明天就国庆了,今晚去花城广场那片溜达了一下,发现外面的气氛真浓郁呀,于是毅然决然决定明天继续实验室搬砖的一天!国庆快乐呀!冲鸭~