Skip to content

Commit 7ed5a0d

Browse files
committed
添加 problem 470、717、920
1 parent d97d21a commit 7ed5a0d

File tree

9 files changed

+392
-0
lines changed

9 files changed

+392
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package leetcode
2+
3+
import "math/rand"
4+
5+
func rand10() int {
6+
rand10 := 10
7+
for rand10 >= 10 {
8+
rand10 = (rand7() - 1) + rand7()
9+
}
10+
return rand10%10 + 1
11+
}
12+
13+
func rand7() int {
14+
return rand.Intn(7)
15+
}
16+
17+
func rand101() int {
18+
rand40 := 40
19+
for rand40 >= 40 {
20+
rand40 = (rand7()-1)*7 + rand7() - 1
21+
}
22+
return rand40%10 + 1
23+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question470 struct {
9+
para470
10+
ans470
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para470 struct {
16+
}
17+
18+
// ans 是答案
19+
// one 代表第一个答案
20+
type ans470 struct {
21+
one int
22+
}
23+
24+
func Test_Problem470(t *testing.T) {
25+
26+
qs := []question470{
27+
28+
question470{
29+
para470{},
30+
ans470{2},
31+
},
32+
33+
question470{
34+
para470{},
35+
ans470{0},
36+
},
37+
38+
question470{
39+
para470{},
40+
ans470{1},
41+
},
42+
}
43+
44+
fmt.Printf("------------------------Leetcode Problem 470------------------------\n")
45+
46+
for _, q := range qs {
47+
_, p := q.ans470, q.para470
48+
fmt.Printf("【input】:%v 【output】:%v\n", p, rand10())
49+
}
50+
fmt.Printf("\n\n\n")
51+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# [470. Implement Rand10() Using Rand7()](https://leetcode.com/problems/implement-rand10-using-rand7/)
2+
3+
4+
## 题目:
5+
6+
Given a function `rand7` which generates a uniform random integer in the range 1 to 7, write a function `rand10` which generates a uniform random integer in the range 1 to 10.
7+
8+
Do NOT use system's `Math.random()`.
9+
10+
**Example 1:**
11+
12+
Input: 1
13+
Output: [7]
14+
15+
**Example 2:**
16+
17+
Input: 2
18+
Output: [8,4]
19+
20+
**Example 3:**
21+
22+
Input: 3
23+
Output: [8,1,10]
24+
25+
**Note:**
26+
27+
1. `rand7` is predefined.
28+
2. Each testcase has one argument: `n`, the number of times that `rand10` is called.
29+
30+
**Follow up:**
31+
32+
1. What is the [expected value](https://en.wikipedia.org/wiki/Expected_value) for the number of calls to `rand7()` function?
33+
2. Could you minimize the number of calls to `rand7()`?
34+
35+
36+
## 题目大意
37+
38+
已有方法 rand7 可生成 1 到 7 范围内的均匀随机整数,试写一个方法 rand10 生成 1 到 10 范围内的均匀随机整数。不要使用系统的 Math.random() 方法。
39+
40+
41+
提示:
42+
43+
- rand7 已定义。
44+
- 传入参数: n 表示 rand10 的调用次数。
45+
46+
47+
进阶:
48+
49+
- rand7()调用次数的 期望值 是多少 ?
50+
- 你能否尽量少调用 rand7() ?
51+
52+
53+
54+
## 解题思路
55+
56+
57+
- 给出 `rand7()` 要求实现 `rand10()`
58+
- `rand7()` 等概率地产生1,2,3,4,5,6,7。要想得到 `rand10()` 即等概率的生成 1-10 。解题思路是先构造一个 `randN()`,这个 N 必须是 10 的整数倍,然后 randN % 10 就可以得到 `rand10()` 了。所以可以从 `rand7()` 先构造出 `rand49()`,再把 `rand49()` 中大于等于 40 的都过滤掉,这样就得到了 `rand40()`,在对 10 取余即可。
59+
- 具体构造步骤,`rand7() --> rand49() --> rand40() --> rand10()`
60+
1. `rand7()` 等概率地产生 1,2,3,4,5,6,7.
61+
2. `rand7() - 1` 等概率地产生 [0,6].
62+
3. `(rand7() - 1) *7` 等概率地产生 0, 7, 14, 21, 28, 35, 42
63+
4. `(rand7() - 1) * 7 + (rand7() - 1)` 等概率地产生 [0, 48] 这 49 个数字
64+
5. 如果步骤 4 的结果大于等于 40,那么就重复步骤 4,直到产生的数小于 40
65+
6. 把步骤 5 的结果 mod 10 再加 1, 就会等概率的随机生成 [1, 10]
66+
- 这道题可以推广到生成任意数的随机数问题。用 `randN()` 实现 `randM()``M>N`。步骤如下:
67+
1.`randN()` 先实现 `randX()`,其中 X ≥ M,X 是 M 的整数倍。如这题中的 49 > 10;
68+
2. 再用 `randX()` 生成 `randM()`,如这题中的 49 —> 40 —> 10。
69+
- 举个例子,用 `rand3()` 生成 `rand11()`,可以先生成 `rand27()`,然后变成以 22 为界限,因为 22 是 11 的倍数。生成 `rand27()` 的方式:`3 * 3 * (rand3() - 1) + 3 * (rand3() - 1) + (rand3() - 1)`,最后生成了 `rand11()`;用 `rand7()` 生成 `rand9()`,可以先生成 `rand49()`,然后变成以 45 为界限,因为 45 是 9 的倍数。生成 `rand49()` 的方式:`(rand7() - 1) * 7 + (rand7() - 1)`,最后生成了 `rand9()`;用 `rand6()` 生成 `rand13()`,可以先生成 `rand36()`,然后变成以 26 为界限,因为 26 是 13 的倍数。生成 `rand36()` 的方式:`(rand6() - 1) * 6 + (rand6() - 1)`,最后生成了 `rand13()`
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package leetcode
2+
3+
func isOneBitCharacter(bits []int) bool {
4+
var i int
5+
for i = 0; i < len(bits)-1; i++ {
6+
if bits[i] == 1 {
7+
i++
8+
}
9+
}
10+
return i == len(bits)-1
11+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question717 struct {
9+
para717
10+
ans717
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para717 struct {
16+
one []int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans717 struct {
22+
one bool
23+
}
24+
25+
func Test_Problem717(t *testing.T) {
26+
27+
qs := []question717{
28+
29+
question717{
30+
para717{[]int{1, 0, 0}},
31+
ans717{true},
32+
},
33+
34+
question717{
35+
para717{[]int{1, 1, 1, 0}},
36+
ans717{false},
37+
},
38+
39+
question717{
40+
para717{[]int{0, 1, 1, 1, 0, 0}},
41+
ans717{true},
42+
},
43+
44+
question717{
45+
para717{[]int{1, 1, 1, 1, 0}},
46+
ans717{true},
47+
},
48+
}
49+
50+
fmt.Printf("------------------------Leetcode Problem 717------------------------\n")
51+
52+
for _, q := range qs {
53+
_, p := q.ans717, q.para717
54+
fmt.Printf("【input】:%v 【output】:%v\n", p, isOneBitCharacter(p.one))
55+
}
56+
fmt.Printf("\n\n\n")
57+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# 717. 1-bit and 2-bit Characters
2+
3+
Created Time: Jul 25, 2019 10:23 AM
4+
Difficulty: Easy
5+
Link: https://leetcode.com/problems/1-bit-and-2-bit-characters/
6+
Tags: Array
7+
8+
# 题目:
9+
10+
We have two special characters. The first character can be represented by one bit `0`. The second character can be represented by two bits (`10` or `11`).
11+
12+
Now given a string represented by several bits. Return whether the last character must be a one-bit character or not. The given string will always end with a zero.
13+
14+
**Example 1:**
15+
16+
Input:
17+
bits = [1, 0, 0]
18+
Output: True
19+
Explanation:
20+
The only way to decode it is two-bit character and one-bit character. So the last character is one-bit character.
21+
22+
**Example 2:**
23+
24+
Input:
25+
bits = [1, 1, 1, 0]
26+
Output: False
27+
Explanation:
28+
The only way to decode it is two-bit character and two-bit character. So the last character is NOT one-bit character.
29+
30+
**Note:**
31+
32+
- `1 <= len(bits) <= 1000`.
33+
- `bits[i]` is always `0` or `1`.
34+
35+
## 题目大意
36+
37+
有两种特殊字符。第一种字符可以用一比特0来表示。第二种字符可以用两比特(10 或 11)来表示。
38+
39+
现给一个由若干比特组成的字符串。问最后一个字符是否必定为一个一比特字符。给定的字符串总是由0结束。
40+
41+
注意:
42+
43+
- 1 <= len(bits) <= 1000.
44+
- bits[i] 总是0 或 1.
45+
46+
47+
## 解题思路
48+
49+
- 给出一个数组,数组里面的元素只有 0 和 1,并且数组的最后一个元素一定是 0。有 2 种特殊的字符,第一类字符是 "0",第二类字符是 "11" 和 "10",请判断这个数组最后一个元素是否一定是属于第一类字符?
50+
- 依题意, 0 的来源有 2 处,可以是第一类字符,也可以是第二类字符,1 的来源只有 1 处,一定出自第二类字符。最后一个 0 当前仅当为第一类字符的情况有 2 种,第一种情况,前面出现有 0,但是 0 和 1 配对形成了第二类字符。第二种情况,前面没有出现 0 。这两种情况的共同点是除去最后一个元素,数组中前面所有的1 都“结对子”。所以利用第二类字符的特征,"1X",遍历整个数组,如果遇到 "1",就跳 2 步,因为 1 后面出现什么数字( 0 或者 1 )并不需要关心。如果 `i` 能在 `len(bits) - 1` 的地方`(数组最后一个元素)`停下,那么对应的是情况一或者情况二,前面的 0 都和 1 匹配上了,最后一个 0 一定是第一类字符。如果 `i``len(bit)` 的位置`(超出数组下标)`停下,说明 `bits[len(bits) - 1] == 1`,这个时候最后一个 0 一定属于第二类字符。
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package leetcode
2+
3+
func numMusicPlaylists(N int, L int, K int) int {
4+
dp, mod := make([][]int, L+1), 1000000007
5+
for i := 0; i < L+1; i++ {
6+
dp[i] = make([]int, N+1)
7+
}
8+
dp[0][0] = 1
9+
for i := 1; i <= L; i++ {
10+
for j := 1; j <= N; j++ {
11+
dp[i][j] = (dp[i-1][j-1] * (N - (j - 1))) % mod
12+
if j > K {
13+
dp[i][j] = (dp[i][j] + (dp[i-1][j]*(j-K))%mod) % mod
14+
}
15+
}
16+
}
17+
return dp[L][N]
18+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question920 struct {
9+
para920
10+
ans920
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para920 struct {
16+
N int
17+
L int
18+
K int
19+
}
20+
21+
// ans 是答案
22+
// one 代表第一个答案
23+
type ans920 struct {
24+
one int
25+
}
26+
27+
func Test_Problem920(t *testing.T) {
28+
29+
qs := []question920{
30+
31+
question920{
32+
para920{3, 3, 1},
33+
ans920{6},
34+
},
35+
36+
question920{
37+
para920{2, 3, 0},
38+
ans920{6},
39+
},
40+
41+
question920{
42+
para920{2, 3, 1},
43+
ans920{2},
44+
},
45+
}
46+
47+
fmt.Printf("------------------------Leetcode Problem 920------------------------\n")
48+
49+
for _, q := range qs {
50+
_, p := q.ans920, q.para920
51+
fmt.Printf("【input】:%v 【output】:%v\n", p, numMusicPlaylists(p.N, p.L, p.K))
52+
}
53+
fmt.Printf("\n\n\n")
54+
}

0 commit comments

Comments
 (0)