当前位置:网站首页>剑指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;
}
}
边栏推荐
猜你喜欢
随机推荐
TOGAF之架构标准规范(一)
Shell重油常压塔模拟仿真与控制
Interviewer, don't ask me to shake hands three times and wave four times again
Temporal介绍
CentOS7 - yum install mysql
pycharm专业版使用
【MQ我可以讲一个小时】
1. 获取数据-requests.get()
Tapdata 与 Apache Doris 完成兼容性互认证,共建新一代数据架构
MySQL forgot password
mysql stored procedure
再见了繁琐的Excel,掌握数据分析处理技术就靠它了
About the problems encountered by Xiaobai installing nodejs (npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
<urlopen error [Errno 11001] getaddrinfo failed>的解决、isinstance()函数初略介绍
Simple read operation of EasyExcel
MySQL transaction isolation level, rounding
MySQL事务隔离级别详解
MySQL8.0.26安装配置教程(windows 64位)
mysql uses on duplicate key update to update data in batches
sql statement - how to query data in another table based on the data in one table








