当前位置:网站首页>剑指offer基础版 ---- 第29天
剑指offer基础版 ---- 第29天
2022-07-31 05:09:00 【米兰的小红黑】
class Solution {
public boolean isMatch(String s, String p) {
int m = s.length();
int n = p.length();
boolean[][] f = new boolean[m + 1][n + 1];
f[0][0] = true;
for (int i = 0; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (p.charAt(j - 1) == '*') {
f[i][j] = f[i][j - 2];
if (matches(s, p, i, j - 1)) {
f[i][j] = f[i][j] || f[i - 1][j];
}
} else {
if (matches(s, p, i, j)) {
f[i][j] = f[i - 1][j - 1];
}
}
}
}
return f[m][n];
}
public boolean matches(String s, String p, int i, int j) {
if (i == 0) {
return false;
}
if (p.charAt(j - 1) == '.') {
return true;
}
return s.charAt(i - 1) == p.charAt(j - 1);
}
}
class Solution {
public int nthUglyNumber(int n) {
int[] factors = {
2, 3, 5};
Set<Long> seen = new HashSet<Long>();
PriorityQueue<Long> heap = new PriorityQueue<Long>();
seen.add(1L);
heap.offer(1L);
int ugly = 0;
for (int i = 0; i < n; i++) {
long curr = heap.poll();
ugly = (int) curr;
for (int factor : factors) {
long next = curr * factor;
if (seen.add(next)) {
heap.offer(next);
}
}
}
return ugly;
}
}
class Solution {
public double[] dicesProbability(int n) {
double[] dp = new double[6];
Arrays.fill(dp, 1.0 / 6.0);
for (int i = 2; i <= n; i++) {
double[] tmp = new double[5 * i + 1];
for (int j = 0; j < dp.length; j++) {
for (int k = 0; k < 6; k++) {
tmp[j + k] += dp[j] / 6.0;
}
}
dp = tmp;
}
return dp;
}
}
边栏推荐
- Interviewer, don't ask me to shake hands three times and wave four times again
- 关于LocalDateTime的全局返回时间带“T“的时间格式处理
- MySQL事务隔离级别详解
- 工作流编排引擎-Temporal
- MySQL开窗函数
- <urlopen error [Errno 11001] getaddrinfo failed>的解决、isinstance()函数初略介绍
- The interviewer asked me TCP three handshake and four wave, I really
- SQL statement to range query time field
- SQL语句中对时间字段进行区间查询
- C语言的文件操作(一)
猜你喜欢
随机推荐
C语言指针详解
Duplicate entry 'XXX' for key 'XXX.PRIMARY' solution.
Minesweeper game (written in c language)
如何将项目部署到服务器上(全套教程)
剑指offer专项突击版 ---- 第 6 天
Numpy中np.meshgrid的简单用法示例
面试官问我TCP三次握手和四次挥手,我真的是
【ORACLE Explain 详解】
Typec手机有线网卡网线转网口转接口快充方案
Information System Project Manager Core Test Site (55) Configuration Manager (CMO) Work
Moment Pool Cloud quickly installs packages such as torch-sparse and torch-geometric
mysql5.7.35安装配置教程【超级详细安装教程】
Tapdata 与 Apache Doris 完成兼容性互认证,共建新一代数据架构
Simple read operation of EasyExcel
matlab simulink欠驱动水面船舶航迹自抗扰控制研究
Mysql application cannot find my.ini file after installation
Linux系统安装mysql(rpm方式安装)
DVWA靶场环境搭建
MySQL optimization: from ten seconds to three hundred milliseconds
SQL语句中对时间字段进行区间查询