Skip to content

Commit 2bee377

Browse files
dwdw
authored andcommitted
Merge branch 'master' of github.com:muyids/leetcode
2 parents 3197391 + da4da81 commit 2bee377

File tree

11 files changed

+424
-105
lines changed

11 files changed

+424
-105
lines changed

‎README.md‎

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -97,26 +97,6 @@ collect some good articles about data structure and algorithm.
9797
* [线段树](https://muyids.github.io/simple-algorithm/chapter/线段树.html)
9898
* [ST表](https://muyids.github.io/simple-algorithm/chapter/ST表.html)
9999

100-
## 每日打卡
101-
102-
获取今日打卡题目
103-
104-
```shell
105-
npm run card
106-
```
107-
108-
输出
109-
110-
```cpp
111-
在岸人民币汇率 2020-03-10 23:29:56 收盘价: 6.957
112-
根据汇率计算得今日打卡题目
113-
1 题: 500
114-
2 题: 964
115-
3 题: 1222
116-
```
117-
118-
根据上一天在岸人民币汇率收盘价作为随机数种子,随机三道题目
119-
120100
## 🙉 说明
121101

122102
* 本项目大部分题目来源:[力扣(LeetCode)](https://leetcode-cn.com),少部分来源[acwing](https://www.acwing.com/)

‎TOC-By-ID.md‎

Lines changed: 33 additions & 29 deletions
Large diffs are not rendered by default.

‎TOC-By-Tag.md‎

Lines changed: 54 additions & 50 deletions
Large diffs are not rendered by default.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
最初在一个记事本上只有一个字符 'A'。你每次可以对这个记事本进行两种操作:
2+
3+
Copy All (复制全部) : 你可以复制这个记事本中的所有字符(部分的复制是不允许的)。
4+
Paste (粘贴) : 你可以粘贴你上一次复制的字符。
5+
给定一个数字 n 。你需要使用最少的操作次数,在记事本中打印出恰好 n 个 'A'。输出能够打印出 n 个 'A' 的最少操作次数。
6+
7+
```cpp
8+
示例 1:
9+
10+
输入: 3
11+
输出: 3
12+
解释:
13+
最初, 我们只有一个字符 'A'
14+
1 步, 我们使用 Copy All 操作。
15+
2 步, 我们使用 Paste 操作来获得 'AA'
16+
3 步, 我们使用 Paste 操作来获得 'AAA'
17+
18+
```
19+
20+
说明:
21+
22+
- n 的取值范围是 [1, 1000]
23+
24+
---
25+
26+
## dfs暴搜
27+
28+
数据规模 n[1, 1000],可以暴搜
29+
30+
```cpp
31+
class Solution {
32+
public:
33+
int ans = INT_MAX;
34+
int minSteps(int n) {
35+
if (n == 1) return 0;
36+
dfs(n, 1, 1, 1);
37+
return ans;
38+
}
39+
40+
void dfs(int n, int cur, int buf, int op){ // n个,cur当前大小,buf缓冲区,op操作次数
41+
if (cur == n){
42+
ans = min(ans, op);
43+
return;
44+
} else if (cur > n) return ;
45+
46+
// 复制操作
47+
if (cur != buf) dfs(n, cur, cur, op+1);
48+
49+
// 粘贴操作
50+
dfs(n, cur+buf, buf, op+1);
51+
}
52+
};
53+
```
54+
55+
## dp
56+

‎algorithms/601-700/679.24-game.md‎

Whitespace-only changes.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
有两种特殊字符。第一种字符可以用一比特0来表示。第二种字符可以用两比特(10 或 11)来表示。
2+
3+
现给一个由若干比特组成的字符串。问最后一个字符是否必定为一个一比特字符。给定的字符串总是由0结束。
4+
5+
```cpp
6+
示例 1:
7+
8+
输入:
9+
bits = [1, 0, 0]
10+
输出: True
11+
解释:
12+
唯一的编码方式是一个两比特字符和一个一比特字符。所以最后一个字符是一比特字符。
13+
示例 2:
14+
15+
输入:
16+
bits = [1, 1, 1, 0]
17+
输出: False
18+
解释:
19+
唯一的编码方式是两比特字符和两比特字符。所以最后一个字符不是一比特字符。
20+
```
21+
22+
注意:
23+
24+
- 1 <= len(bits) <= 1000.
25+
- bits[i] 总是0 或 1.
26+
27+
---
28+
29+
## 线性dp
30+
31+
dp[i][0,1]表示以i结尾,长度为0或1的子串是否存在
32+
33+
```cpp
34+
class Solution {
35+
public:
36+
bool isOneBitCharacter(vector<int>& bits) {
37+
int n = bits.size();
38+
if (n==0) return false;
39+
if (bits.back() == 1) return false;
40+
if (n==1) return bits.back() == 0;
41+
vector<vector<bool>> dp(n, vector<bool>(2, false));
42+
if (bits[0]==0)dp[0][0] = true;
43+
if (bits[1] == 0) dp[1][0] = dp[0][0];
44+
if (bits[0] == 1) dp[1][1] = true;
45+
for (int i = 2; i<n; i++) {
46+
dp[i][0] = bits[i] == 0 && (dp[i-1][0] || dp[i-1][1]);
47+
dp[i][1] = bits[i-1] == 1 && (dp[i-2][0] || dp[i-2][1]);
48+
}
49+
return dp[n-1][0];
50+
}
51+
};
52+
```
53+
54+
55+
56+
## 找规律
57+
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
2+
给定一个由表示变量之间关系的字符串方程组成的数组,每个字符串方程 equations[i] 的长度为 4,并采用两种不同的形式之一:"a==b" 或 "a!=b"。在这里,a 和 b 是小写字母(不一定不同),表示单字母变量名。
3+
4+
只有当可以将整数分配给变量名,以便满足所有给定的方程时才返回 true,否则返回 false。 
5+
 
6+
```case
7+
示例 1:
8+
9+
输入:["a==b","b!=a"]
10+
输出:false
11+
解释:如果我们指定,a = 1 且 b = 1,那么可以满足第一个方程,但无法满足第二个方程。没有办法分配变量同时满足这两个方程。
12+
示例 2:
13+
14+
输入:["b==a","a==b"]
15+
输出:true
16+
解释:我们可以指定 a = 1 且 b = 1 以满足满足这两个方程。
17+
示例 3:
18+
19+
输入:["a==b","b==c","a==c"]
20+
输出:true
21+
示例 4:
22+
23+
输入:["a==b","b!=c","c==a"]
24+
输出:false
25+
示例 5:
26+
27+
输入:["c==c","b==d","x!=z"]
28+
输出:true
29+
```
30+
31+
提示:
32+
33+
- 1 <= equations.length <= 500
34+
- equations[i].length == 4
35+
- equations[i][0] 和 equations[i][3] 是小写字母
36+
- equations[i][1] 要么是 '=',要么是 '!'
37+
- equations[i][2] 是 '='
38+
39+
40+
---
41+
42+
## 解题思路
43+
44+
并查集
45+
46+
- 数据模型:26个字母用数组A[26]表示,初始化为0-25
47+
- 先将所有等式的左右两边合并
48+
- 遍历所有不等式,如果左右两边在一个集合,则返回false
49+
50+
## 代码实现
51+
52+
```cpp
53+
54+
class Solution {
55+
public:
56+
int f[26];
57+
bool equationsPossible(vector<string>& equations) {
58+
for (int i =0; i < 26; i++)f[i] = i;
59+
for (auto &s: equations){
60+
if (s[1] == '=' && s[0] != s[3]){
61+
if (find(s[0] - 'a') != find(s[3]-'a')) u(s[0]-'a' , s[3]-'a');
62+
}
63+
}
64+
for (auto &s: equations){
65+
if (s[1] == '!' && find(s[0] - 'a') == find(s[3]-'a')) return false;
66+
}
67+
return true;
68+
}
69+
70+
int find(int x){
71+
if (f[x] == x) return x;
72+
return f[x] = find(f[x]);
73+
}
74+
75+
void u(int x, int y){
76+
f[find(x)] = find(y);
77+
}
78+
};
79+
```
80+

‎chapter/dp/线性dp.md‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ f(i) 表示凑出i元所有的最少硬币数(凑出i元的方案数)
4444

4545
### 最长序列
4646

47-
4847
- [LeetCode 300. Longest Increasing Subsequence (medium)](https://github.com/muyids/leetcode/blob/master/algorithms/201-300/300.longest-increasing-subsequence.md)
4948

5049
- [LeetCode 1143. Longest Common Subsequence (medium)](https://github.com/muyids/leetcode/blob/master/algorithms/1101-1200/1143.longest-common-subsequence.md)
@@ -72,15 +71,15 @@ f(i) 表示凑出i元所有的最少硬币数(凑出i元的方案数)
7271

7372
- [LeetCode 265. Paint House II (hard)](https://github.com/muyids/leetcode/blob/master/algorithms/201-300/265.paint-house-ii.md)
7473

75-
## 打家劫舍系列
74+
### 打家劫舍系列
7675

7776
- [LeetCode 198. House Robber (easy)](https://github.com/muyids/leetcode/blob/master/algorithms/101-200/198.house-robber.md)
7877

7978
- [LeetCode 213. House Robber II (medium)](https://github.com/muyids/leetcode/blob/master/algorithms/201-300/213.house-robber-ii.md)
8079

8180
打家劫舍3 是树形DP
8281

83-
## 股票系列
82+
### 股票系列
8483

8584
- [LeetCode 121. Best Time to Buy and Sell Stock (easy)](https://github.com/muyids/leetcode/blob/master/algorithms/101-200/121.best-time-to-buy-and-sell-stock.md)
8685

@@ -94,7 +93,7 @@ f(i) 表示凑出i元所有的最少硬币数(凑出i元的方案数)
9493

9594
- [LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee (medium)](https://github.com/muyids/leetcode/blob/master/algorithms/701-800/714.best-time-to-buy-and-sell-stock-with-transaction-fee.md)
9695

97-
## 字符串匹配系列
96+
### 字符串匹配系列
9897

9998
- [LeetCode 10. Regular Expression Matching (hard)](https://github.com/muyids/leetcode/blob/master/algorithms/1-100/10.regular-expression-matching.md)
10099

‎chapter/math/计算几何.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@
1818

1919
- [LeetCode 963. Minimum Area Rectangle II (medium)](https://github.com/muyids/leetcode/blob/master/algorithms/901-1000/963.minimum-area-rectangle-ii.md)
2020

21-
- [面试题 16.14. 最佳直线](https://github.com/muyids/leetcode/blob/master/lcci/best-line-lcci.md)
21+
- [面试题 16.14. 最佳直线](https://github.com/muyids/leetcode/blob/master/lcci/best-line-lcci.md)
22+
23+
- [面试题 16.03. 交点](https://github.com/muyids/leetcode/blob/master/lcci/intersection-lcci.md)

0 commit comments

Comments
 (0)