当前位置:网站首页>剑指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;
}
}
边栏推荐
- Moment Pool Cloud quickly installs packages such as torch-sparse and torch-geometric
- MySQL window function
- Anaconda配置环境指令
- Unity Framework Design Series: How Unity Designs Network Frameworks
- 12 reasons for MySQL slow query
- 面试官,不要再问我三次握手和四次挥手
- Why use Flink and how to get started with Flink?
- Refinement of the four major collection frameworks: Summary of List core knowledge
- 快速掌握并发编程 --- 基础篇
- TOGAF之架构标准规范(一)
猜你喜欢
Numpy中np.meshgrid的简单用法示例
面试官:生成订单30分钟未支付,则自动取消,该怎么实现?
Minesweeper game (written in c language)
2022-07-30:以下go语言代码输出什么?A:[]byte{} []byte;B:[]byte{} []uint8;C:[]uint8{} []byte;D:[]uin8{} []uint8。
Redis进阶 - 缓存问题:一致性、穿击、穿透、雪崩、污染等.
SQL injection of DVWA
目标检测学习笔记
matlab abel变换图片处理
城市内涝及桥洞隧道积水在线监测系统
再见了繁琐的Excel,掌握数据分析处理技术就靠它了
随机推荐
Summary of MySQL common interview questions (recommended collection!!!)
Centos7 install mysql5.7
numpy和pytorch中的元素拼接操作:stack,concatenat,cat
如何将项目部署到服务器上(全套教程)
Three oj questions on leetcode
城市内涝及桥洞隧道积水在线监测系统
【ORACLE Explain 详解】
MySQL8--Windows下使用压缩包安装的方法
Why use Flink and how to get started with Flink?
docker安装postgresSQL和设置自定义数据目录
STM32 - DMA
CentOS7 —— yum安装mysql
Minesweeper game (written in c language)
MySQL window function
The interviewer asked me TCP three handshake and four wave, I really
.NET-6.WinForm2.NanUI learning and summary
工作流编排引擎-Temporal
SQL statement to range query time field
TOGAF之架构标准规范(一)
Interview Redis High Reliability | Master-Slave Mode, Sentinel Mode, Cluster Cluster Mode