【Q10】正則表達式
Given an input string (s
) and a pattern (p
), implement regular expression matching with support for '.'
and '*'
.express
'.' Matches any single character. '*' Matches zero or more of the preceding element.
The matching should cover the entire input string (not partial).app
Note:this
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 1:spa
Input: s = "aa" p = "a" Output: false Explanation: "a" does not match the entire string "aa".
Example 2:code
Input: s = "aa" p = "a*" Output: true Explanation: '*' means zero or more of the precedeng element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
Example 3:orm
Input: s = "ab" p = ".*" Output: true Explanation: ".*" means "zero or more (*) of any character (.)".
Example 4:blog
Input: s = "aab" p = "c*a*b" Output: true Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab".
Example 5:ip
Input: s = "mississippi" p = "mis*is*p*." Output: false
Solution: 用正則表達式,解法比較做弊,詳細的動態規劃解法見連接:
https://leetcode.com/problems/regular-expression-matching/discuss/5723/My-DP-approach-in-Python-with-comments-and-unittest
(1)動態規劃
import unittest class Solution(object): def isMatch(self, s, p): # The DP table and the string s and p use the same indexes i and j, but # table[i][j] means the match status between p[:i] and s[:j], i.e. # table[0][0] means the match status of two empty strings, and # table[1][1] means the match status of p[0] and s[0]. Therefore, when # refering to the i-th and the j-th characters of p and s for updating # table[i][j], we use p[i - 1] and s[j - 1]. # Initialize the table with False. The first row is satisfied. table = [[False] * (len(s) + 1) for _ in range(len(p) + 1)] # Update the corner case of matching two empty strings. table[0][0] = True # Update the corner case of when s is an empty string but p is not. # Since each '*' can eliminate the charter before it, the table is # vertically updated by the one before previous. [test_symbol_0] for i in range(2, len(p) + 1): table[i][0] = table[i - 2][0] and p[i - 1] == '*' for i in range(1, len(p) + 1): for j in range(1, len(s) + 1): if p[i - 1] != "*": # Update the table by referring the diagonal element. table[i][j] = table[i - 1][j - 1] and \ (p[i - 1] == s[j - 1] or p[i - 1] == '.') else: # Eliminations (referring to the vertical element) # Either refer to the one before previous or the previous. # I.e. * eliminate the previous or count the previous. # [test_symbol_1] table[i][j] = table[i - 2][j] or table[i - 1][j] # Propagations (referring to the horizontal element) # If p's previous one is equal to the current s, with # helps of *, the status can be propagated from the left. # [test_symbol_2] if p[i - 2] == s[j - 1] or p[i - 2] == '.': table[i][j] |= table[i][j - 1] return table[-1][-1]
(2)正則表達式:ci
class Solution: def isMatch(self, s, p): import re return re.match('^'+p+'$',s)!=None
【Q11】
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). nvertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container and n is at least 2.
The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
Example:
Input: [1,8,6,2,5,4,8,3,7] Output: 49
解法:從左右兩邊依次向裏遞進,聲明兩個變量area(存儲當前水槽容量)和maxArea(存儲水槽歷史最大容量)。
因爲水槽中的水總量老是由左右兩柱中較低的那一個決定,因此每次計算完當前水槽總量後,將較低位向裏推動,直至到達中心位置處,搜索完畢。
計算量爲O(n)
class Solution: def maxArea(self, height): """ :type height: List[int] :rtype: int """ maxArea = 0 l = 0 r = len(height)-1 while l<r: if height[l]<height[r]: area = (r-l)*height[l] l += 1 else: area = (r-l)*height[r] r -= 1 if area>maxArea: maxArea = area return maxArea
【Q12】
Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000
For example, two is written as II
in Roman numeral, just two one's added together. Twelve is written as, XII
, which is simply X
+ II
. The number twenty seven is written as XXVII
, which is XX
+ V
+ II
.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII
. Instead, the number four is written as IV
. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX
. There are six instances where subtraction is used:
I
can be placed before V
(5) and X
(10) to make 4 and 9. X
can be placed before L
(50) and C
(100) to make 40 and 90. C
can be placed before D
(500) and M
(1000) to make 400 and 900.Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.
Example 1:
Input: 3 Output: "III"
Example 2:
Input: 4 Output: "IV"
Example 3:
Input: 9 Output: "IX"
Example 4:
Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3.
Example 5:
Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
解法:這題很迷,窮舉法速度最快
class Solution: def intToRoman(self, num): """ :type num: int :rtype: str """ I = ['','I','II','III','IV','V','VI','VII','VIII','IX'] X = ['','X','XX','XXX','XL','L','LX','LXX','LXXX','XC'] C = ['','C','CC','CCC','CD','D','DC','DCC','DCCC','CM'] M = ['','M','MM','MMM'] return M[num//1000]+C[(num//100)%10]+X[(num//10)%10]+I[num%10]