File tree 4 files changed +99
-2
lines changed
4 files changed +99
-2
lines changed Original file line number Diff line number Diff line change
1
+ package offer ;
2
+
3
+ /**
4
+ * @author : CodeWater
5
+ * @create :2022-07-21-21:47
6
+ * @Function Description :19.正则表达式匹配
7
+ */
8
+
9
+ public class _19RegularExpressionMatching {
10
+ class Solution {
11
+ public boolean isMatch (String s , String p ) {
12
+ int m = s .length () + 1 , n = p .length () + 1 ;
13
+ boolean [][] dp = new boolean [m ][n ];
14
+ dp [0 ][0 ] = true ;
15
+ // 初始化首行
16
+ for ( int j = 2 ; j < n ; j += 2 ){
17
+ dp [0 ][j ] = dp [0 ][j - 2 ] && p .charAt ( j - 1 ) == '*' ;
18
+ }
19
+
20
+ for ( int i = 1 ; i < m ; i ++ ){
21
+ for ( int j = 1 ; j < n ; j ++ ){
22
+ /*//==========================ifelse写法============================
23
+ * if(p.charAt(j - 1) == '*') {
24
+ if(dp[i][j - 2]) dp[i][j] = true; // 1.
25
+ else if(dp[i - 1][j] && s.charAt(i - 1) == p.charAt(j - 2)) dp[i][j] = true; // 2.
26
+ else if(dp[i - 1][j] && p.charAt(j - 2) == '.') dp[i][j] = true; // 3.
27
+ } else {
28
+ if(dp[i - 1][j - 1] && s.charAt(i - 1) == p.charAt(j - 1)) dp[i][j] = true; // 1.
29
+ else if(dp[i - 1][j - 1] && p.charAt(j - 1) == '.') dp[i][j] = true; // 2.
30
+ }
31
+ * */
32
+ dp [i ][j ] = p .charAt (j - 1 ) == '*' ?
33
+ dp [i ][j - 2 ] || dp [i - 1 ][j ] && (s .charAt (i - 1 ) == p .charAt (j - 2 ) || p .charAt (j - 2 ) == '.' ) :
34
+ dp [i - 1 ][j - 1 ] && (p .charAt (j - 1 ) == '.' || s .charAt (i - 1 ) == p .charAt (j - 1 ));
35
+
36
+ }
37
+ }
38
+
39
+ return dp [m - 1 ][n - 1 ];
40
+ }
41
+ }
42
+ }
Original file line number Diff line number Diff line change @@ -13,7 +13,7 @@ public class Codec {
13
13
public String serialize (TreeNode root ) {
14
14
if (root == null ) return "[]" ;
15
15
StringBuilder res = new StringBuilder ("[" );
16
- Queue <TreeNode > queue = new LinkedList <>() {{ add (root ); }};
16
+ Queue <TreeNode > queue = new LinkedList <TreeNode >() {{ add (root ); }};
17
17
while (!queue .isEmpty ()) {
18
18
TreeNode node = queue .poll ();
19
19
if (node != null ) {
@@ -32,7 +32,7 @@ public TreeNode deserialize(String data) {
32
32
if (data .equals ("[]" )) return null ;
33
33
String [] vals = data .substring (1 , data .length () - 1 ).split ("," );
34
34
TreeNode root = new TreeNode (Integer .parseInt (vals [0 ]));
35
- Queue <TreeNode > queue = new LinkedList <>() {{ add (root ); }};
35
+ Queue <TreeNode > queue = new LinkedList <TreeNode >() {{ add (root ); }};
36
36
int i = 1 ;
37
37
while (!queue .isEmpty ()) {
38
38
TreeNode node = queue .poll ();
Original file line number Diff line number Diff line change
1
+ package offer ;
2
+
3
+ /**
4
+ * @author : CodeWater
5
+ * @create :2022-07-21-23:32
6
+ * @Function Description :49.丑数
7
+ */
8
+ public class _49UglyNumber {
9
+ class Solution {
10
+ public int nthUglyNumber (int n ) {
11
+ int a = 0 , b = 0 , c = 0 ;
12
+ int [] dp = new int [n ];
13
+ dp [0 ] = 1 ;
14
+ for ( int i = 1 ; i < n ; i ++ ){
15
+ //
16
+ int n2 = dp [a ] * 2 , n3 = dp [b ] * 3 , n5 = dp [c ] * 5 ;
17
+ // 当前数是由上一个数乘三个因子相乘后结果的最小值
18
+ dp [i ] = Math .min ( Math .min ( n2 , n3 ) , n5 );
19
+ // 看看最后结果是n2,n3,还是n5,相应的下标就加加
20
+ if ( dp [i ] == n2 ) a ++;
21
+ if ( dp [i ] == n3 ) b ++;
22
+ if ( dp [i ] == n5 ) c ++;
23
+ }
24
+ return dp [n - 1 ];
25
+ }
26
+ }
27
+ }
Original file line number Diff line number Diff line change
1
+ package offer ;
2
+
3
+ import java .util .Arrays ;
4
+
5
+ /**
6
+ * @author : CodeWater
7
+ * @create :2022-07-21-23:43
8
+ * @Function Description :60.n个骰子的点数
9
+ */
10
+ public class _60NDicePoint {
11
+ class Solution {
12
+ public double [] dicesProbability (int n ) {
13
+ double [] dp = new double [6 ];
14
+ Arrays .fill ( dp , 1.0 / 6.0 );
15
+ for ( int i = 2 ; i <= n ; i ++ ){
16
+ double [] temp = new double [5 * i + 1 ];
17
+ for ( int j = 0 ; j < dp .length ; j ++ ){
18
+ for ( int k = 0 ; k < 6 ; k ++ ){
19
+ temp [j + k ] += dp [j ] / 6.0 ;
20
+ }
21
+ }
22
+ dp = temp ;
23
+ }
24
+
25
+ return dp ;
26
+ }
27
+ }
28
+ }
You can’t perform that action at this time.
0 commit comments