当前位置:网站首页>Leetcode - < dynamic planning special> Jianzhi offer 19, 49, 60
Leetcode - < dynamic planning special> Jianzhi offer 19, 49, 60
2022-07-02 12:21:00 【Ostrich5yw】
The finger of the sword Offer 19. Regular Expression Matching 、49. Ugly number 、60. n The number of dice
Title Description :[19]
Please implement a function to match the contains ’. ‘ and ’‘ Regular expression of . Characters in pattern ’.‘ Represents any character , and ’' Indicates that the character in front of it can appear any number of times ( contain 0 Time ). In the subject , Matching means that all characters of a string match the whole pattern . for example , character string "aaa" With the model "a.a" and "abaca" matching , But with "aa.a" and "ab*a" No match .[49]
We will include only qualitative factors 2、3 and 5 The number of is called ugly (Ugly Number). Seek the order from small to large n Ugly number .[60]
hold n A dice on the ground , The sum of the points on the up side of all dice is s. Input n, Print out s The probability that all possible values of the .
You need to return the answer with an array of floating-point numbers , Among them the first i The elements represent this n The number of points that a die can roll i The probability of the smaller one .
Key points of investigation :
The first 19 topic Two dimensional state array , among dp[i][j]=true Express s[0, i) And p[0, j) Pattern matching
The first 49 topic The three pointers point to 2,3,5 Corresponding position , Put the minimum number of products each time
The first 60 topic Make f(n,s) It's a two-dimensional matrix , Expressed as n The number of dice is s The situation of ; Yes f(n,s)=f(n-1,s-1)+f(n-1,s-2)+f(n-1,s-3)+f(n-1,s-4)+f(n-1,s-5)+f(n-1,s-6)
The first 19 topic
func isMatch(s string, p string) bool {
sLen, pLen := len(s), len(p)
var dp [][]bool
for x := 0; x <= sLen; x++ {
// Create an array of storage states
ar := make([]bool, pLen+1) // among dp[i][j]=true Express s[0, i) And p[0, j) Pattern matching
dp = append(dp, ar)
}
dp[0][0] = true // The boundary conditions , If from 0 At the beginning, the boundary is -1, So we store the results in 1——(Len+1), So pay attention to dp Than s and p More than a
for i := 0; i <= sLen; i++ {
for j := 1; j <= pLen; j++ {
if p[j-1] == '*' {
//dp[i][j-2] Express '*' The number of Representatives is 0, The back says '*' The number of Representatives is 1
dp[i][j] = dp[i][j-2] || (i > 0 && (s[i-1] == p[j-2] || p[j-2] == '.') && dp[i-1][j])
} else {
dp[i][j] = i > 0 && (s[i-1] == p[j-1] || p[j-1] == '.') && dp[i-1][j-1]
}
}
}
return dp[sLen][pLen]
}
The first 49 topic
func nthUglyNumber(n int) int {
res := make([]int, n)
res[0] = 1
p2, p3, p5 := 0, 0, 0
for i := 1;i < n;i ++{
res[i] = min(res[p2] * 2, res[p3] * 3, res[p5] * 5)
if res[i] == res[p2] * 2{
// Not available if else, When multiple minimum values are equal , It should be moved back at the same time to ensure weight removal
p2 ++
}
if res[i] == res[p3] * 3{
p3 ++
}
if res[i] == res[p5] * 5{
p5 ++
}
}
return res[n-1]
}
func min(a, b, c int)int{
if(a < b){
if a < c{
return a
}
return c
}
if b < c{
return b
}
return c
}
The first 60 topic
// Make f(n,s) It's a two-dimensional matrix , Expressed as n The number of dice is s The situation of
func dicesProbability(n int) []float64 {
dp := make([][]int32, 0) // Build a n+1 That's ok ,n*6+1 Array of columns . among +1 It is convenient to handle the boundary .
for i := 0;i < n+1;i ++{
tt := make([]int32, n*6+1)
dp = append(dp, tt)
}
res := make([]float64, 5*n+1) // preservation n The result of a dice
allSituation := math.Pow(6, float64(n)) // The number of occurrences saved in the array , So calculate the total number of times to calculate the probability
for j := 1; j <= 6; j++ {
dp[1][j] = 1
}
for i := 1; i <= n; i++ {
// i A dice
for j := i; j <= n*6; j++ {
// And for j
for k := 1; k <= 6; k++ {
// f(n,s)=f(n-1,s-1)+f(n-1,s-2)+f(n-1,s-3)+f(n-1,s-4)+f(n-1,s-5)+f(n-1,s-6)
if j >= k {
dp[i][j] += dp[i-1][j-k]
}
if i == n {
res[j-i] = float64(dp[i][j]) / allSituation
}
}
}
}
return res
}
边栏推荐
- MSI announced that its motherboard products will cancel all paper accessories
- Writing method of then part in drools
- Simple use of drools decision table
- On data preprocessing in sklearn
- CDA数据分析——AARRR增长模型的介绍、使用
- Map and set
- 考研英语二大作文模板/图表作文,英语图表作文这一篇就够了
- Mysql database foundation
- arcgis js 4. Add pictures to x map
- Leetcode922 sort array by parity II
猜你喜欢

【工控老马】西门子PLC Siemens PLC TCP协议详解

Experiment of connecting mobile phone hotspot based on Arduino and esp8266 (successful)

Simple understanding of ThreadLocal
![[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)](/img/18/f0c9ef6250a717f8e66c95da4de08c.jpg)
[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)

基于Arduino和ESP8266的连接手机热点实验(成功)

Performance tuning project case

Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement

使用Sqoop把ADS层数据导出到MySQL

Map and set

Sweetheart leader: Wang Xinling
随机推荐
arcgis js 4.x 地图中加入图片
甜心教主:王心凌
CONDA common command summary
深入理解P-R曲线、ROC与AUC
(C language) 3 small Codes: 1+2+3+ · · +100=? And judge whether a year is a leap year or a normal year? And calculate the circumference and area of the circle?
PyTorch nn.RNN 参数全解析
CPU指令集介绍
单指令多数据SIMD的SSE/AVX指令集和API
Map和Set
倍增 LCA(最近公共祖先)
测试左移和右移
Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
字符串回文hash 模板题 O(1)判字符串是否回文
5g era, learning audio and video development, a super hot audio and video advanced development and learning classic
二分刷题记录(洛谷题单)区间的甄别
Gaode map test case
LeetCode—<动态规划专项>剑指 Offer 19、49、60
Discrimination of the interval of dichotomy question brushing record (Luogu question sheet)
Drools dynamically add, modify, and delete rules
Experiment of connecting mobile phone hotspot based on Arduino and esp8266 (successful)