Clock day23.

## Topic 1: Swordfinger Offer 19.Regular Expression Matching

Please implement a function to match containment'. 'Regular expressions for and''. Characters in a pattern'.'denote any character, and''denotes that the characters preceding it can occur any number of times (including 0 times). In this topic, matching means that all characters in a string match the entire pattern. For example, the string'a a A' matches the patterns'a.a'and'abaca', but neither'a a.a' nor'ab*a'.

Example 1:

Input:

s = "aa"

p = "a"

Output: false

Interpretation:'a'cannot match the entire string of'a a'.

Example 2:

Input:

s = "aa"

p = "a*"

Output: true

Interpretation: Because'*'means that you can match zero or more of the preceding elements, where the preceding element is'a'. Therefore, the string'a A' can be considered'a'and repeated once.

Example 3:

Input:

s = "ab"

p = "."

Output: true

Interpretation:'. 'means that zero or more ('*') arbitrary characters ('.') can be matched.

Example 4:

Input:

s = "aab"

p = "cab"

Output: true

Interpretation: Because'*'means zero or more, where'c' is zero and'a'is repeated once, the string'aab' can be matched.

Example 5:

Input:

s = "mississippi"

p = "misisp*."

Output: false

s may be empty and contain only lowercase letters from a-z.

p may be empty and contain only lowercase letters from a-z and characters. and, without contiguous''.

Source: LeetCode

Links: https://leetcode-cn.com/problems/zheng-ze-biao-da-shi-pi-pei-lcof

Solving ideas:

Assuming that the main string is A and the pattern string is B, starting from the last step, you need to focus on the last incoming character. Assuming that the length of A is n and B is m, you can focus on who is the last character of the regular expression B. There are three possibilities: normal characters, * and. (point), then you can discuss these three cases.

Big man's puzzle

java code:

class Solution { public boolean isMatch(String A, String B) { int n = A.length(); int m = B.length(); boolean[][] f = new boolean[n + 1][m + 1]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { //Divide into empty regular and non-empty regular if (j == 0) { f[i][j] = i == 0; } else { //Non-empty regular is divided into two cases*and non* if (B.charAt(j - 1) != '*') { if (i > 0 && (A.charAt(i - 1) == B.charAt(j - 1) || B.charAt(j - 1) == '.')) { f[i][j] = f[i - 1][j - 1]; } } else { //Encountered*, divided into two situations, see and don't see //Don't look if (j >= 2) { f[i][j] |= f[i][j - 2]; } //see if (i >= 1 && j >= 2 && (A.charAt(i - 1) == B.charAt(j - 2) || B.charAt(j - 2) == '.')) { f[i][j] |= f[i - 1][j]; } } } } } return f[n][m]; } }

## Question 2: Offer 49. Ugly Number

We call numbers that contain only prime factors 2, 3, and 5 Ugly Numbers. See the nth Ugly Number in order from smallest to largest.

Example:

Input: n = 10

Output: 12

Explanation: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 are the top 10 ugly numbers.

Explain:

1 is an ugly number.

n not more than 1690.

Source: LeetCode

Links: https://leetcode-cn.com/problems/chou-shu-lcof

Solving ideas:

Recursive nature of clowns: clowns contain only factor 2, 3, 52, 3, 5, so there is "clowns==a smaller clowns\times*a factor" (for example, 10 = 5 \times 210=5*2).

java code:

class Solution { public int nthUglyNumber(int n) { int a = 0, b = 0, c = 0; int[] dp = new int[n]; dp[0] = 1; for(int i = 1; i < n; i++) { int n2 = dp[a] * 2, n3 = dp[b] * 3, n5 = dp[c] * 5; dp[i] = Math.min(Math.min(n2, n3), n5); if(dp[i] == n2) a++; if(dp[i] == n3) b++; if(dp[i] == n5) c++; } return dp[n - 1]; } }

## Question 3: Swordfinger Offer 60.Number of points in n dices

Throw n dices on the floor and sum the number of points all dices face up to s. Enter N and print out the probability of occurrence of all possible values of s.

You need a floating-point array to return the answer, where the ith element represents the probability of the ith smallest of the set of points that this n dice can throw.

Example 1:

Input: 1

Output: [0.16667, 0.16667, 0.16667, 0.16667, 0.16667, 0.16667, 0.16667, 0.16667]

Example 2:

Input: 2

Output: [0.02778, 0.05556, 0.08333, 0.11111, 0.13889, 0.16667, 0.13889, 0.11111, 0.08333, 0.05556, 0.02778]

Restrictions:

1 <= n <= 11

Source: LeetCode

Links: https://leetcode-cn.com/problems/nge-tou-zi-de-dian-shu-lcof

Solving ideas:

Set the solution of the input n dices (i.e. the list of probabilities) to f(n), where the probability of Points and X is f(n, x). Find the recursive formula.

java code:

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; } }