【Q10】

Given an input string (s) and a pattern (p), 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 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 precedeng 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 = "c*a*b"
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 = "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)正则表达式:

class Solution:
def isMatch(self, s, p):
import re return re.match('^'+p+'$',s)!=None

【Q11】

Given n non-negative integers a1a2, ..., an , where each represents a point at coordinate (iai). nvertical lines are drawn such that the two endpoints of line i is at (iai) 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]

【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman的更多相关文章

  1. 【leetcode刷题笔记】Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  2. LeetCode第[10]题(Java):Regular Expression Matching

    题目:匹配正则表达式 题目难度:hard 题目内容:Implement regular expression matching with support for '.' and '*'. '.' Ma ...

  3. LeetCode 失败的尝试 10. regular expression matching & 正则

    Regular Expression Matching 看到正则就感觉头大,因为正则用好了就很强大.有挑战的才有意思. 其实没有一点思路.循环的话,不能一一对比,匹配模式解释的是之前的字符.那就先遍历 ...

  4. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  5. 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number

    [Q7]  把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...

  6. 【LeetCode算法题库】Day2:Median of Two Sorted Arrays & Longest Palindromic Substring & ZigZag Conversion

    [Q4] There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of th ...

  7. 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters

    [Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...

  8. 【LeetCode算法题库】Day5:Roman to Integer & Longest Common Prefix & 3Sum

    [Q13] Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Valu ...

  9. LeetCode 10. 正则表达式匹配(Regular Expression Matching)

    题目描述 给定一个字符串 (s) 和一个字符模式 (p).实现支持 '.' 和 '*' 的正则表达式匹配. '.' 匹配任意单个字符. '*' 匹配零个或多个前面的元素. 匹配应该覆盖整个字符串 (s ...

随机推荐

  1. BZOJ 1821 Group 部落划分 并查集

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1821 题目大意: 聪聪研究发现,荒岛野人总是过着群居的生活,但是,并不是整个荒岛上的所 ...

  2. SDWC补题计划

    2018的寒假去了SD的冬令营,因为一班二班难度悬殊,对我很不友好,几乎什么也没学会,但是我把两个班的课件都存了下来,现在慢慢把两个班的例题以及课后题都补一补(毕竟冬令营的钱不能白花). 这些题目横跨 ...

  3. java web开发环境配置系列(一)安装JDK

    在今天,读书有时是件“麻烦”事.它需要你付出时间,付出精力,还要付出一份心境.--仅以<java web开发环境配置系列>来祭奠那逝去的…… 1.下载JDK文件(http://www.or ...

  4. [译]新的CCSDS图像压缩推荐标准

    摘要——空间数据系统咨询委员会(CCSDS)的数据压缩工作组最近通过了图像数据压缩议案,最终版本预计在2005年发布.议案中采用的算法由两部分组成,先是一个对图像的二维离散小波变换,然后是对变换后的数 ...

  5. 浅谈IC行业产业链以及贸易商在产业链中的作用  2008-10-16 12:45[转自Michael的博客]

    随着集成电路行业在中国的迅猛发展, 中国的低成本劳动力和开放的引入外资政策, 使得全球电子产品生产厂商为了降低成本, 增加产品市场竞争力, 纷纷在中国设立生产线, 而中国不断膨胀的购买力也促进了这一产 ...

  6. AngularJS中的按需加载ocLazyLoad插件应用;

    一.前言 ocLoayLoad是AngularJS的模块按需加载器.一般在小型项目里,首次加载页面就下载好所有的资源没有什么大问题.但是当我们的网站渐渐庞大起来,这样子的加载策略让网速初始化速度变得越 ...

  7. PHP中查询一个日期是周几

    PHP查询一个日期是周几 1.date('l'),获取的是英文的星期几.Sunday 到 Saturday date('l', strtotime('2019-4-6')); // Saturday ...

  8. JavaWeb基础—Http协议

    一.什么是Http协议 超文本传输协议的简称,用于定义客户端与web服务器通迅的格式. 关于[标准的HTTP协议是无状态的],请参见:http://www.cnblogs.com/bellkosmos ...

  9. struts2第四天——拦截器和标签库

    一.拦截器(interceptor)概述 struts2是个框架,里面封装了很多功能,封装的很多功能都是在拦截器里面. (属性封装.模型驱动等都是封装在拦截器里面) struts2里面封装了很多功能, ...

  10. 2017-2018-1 20155210 《信息安全系统设计基础》 实现mypwd

    2017-2018-1 20155210 <信息安全系统设计基础> 实现mypwd 作业要求: 1.学习pwd命令 2.研究pwd实现需要的系统调用(man -k; grep),写出伪代码 ...