Skip to content

Commit 08bdc01

Browse files
committedJun 9, 2020
990与46更新
1 parent 7a62401 commit 08bdc01

File tree

5 files changed

+175
-1
lines changed

5 files changed

+175
-1
lines changed
 

‎README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@
44

55
## 分类整理
66

7-
## 位运算
7+
### dfs
8+
9+
+ No.46 [python](python/46.py)
10+
11+
### 并查集
12+
13+
+ No.990 [cpp(未优化)](cpp/990.cpp) [cpp优化](cpp/990_1.cpp)
14+
15+
### 位运算
816

917
+ No.136 [cpp](cpp/136.cpp)
1018
+ No.64 [cpp](cpp/64.cpp) [python](python/64.py)
@@ -32,6 +40,7 @@
3240

3341
## 动态规划
3442

43+
+ No.46 [cpp]() [python](python/46_dp.py)
3544
+ No.53 [cpp](cpp/53.cpp) [python](python/53.py)
3645
+ No.120 [cpp](cpp/120.cpp) [python](python/120.py)
3746
+ No.837 [cpp](cpp/837.cpp) [python](python/837.py)

‎cpp/990.cpp

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <vector>
2+
#include <string>
3+
#include <unordered_map>
4+
using namespace std;
5+
6+
class Solution {
7+
public:
8+
/**
9+
* 执行用时 :16 ms, 在所有 C++ 提交中击败了25.68%的用户
10+
* 内存消耗 :11.2 MB, 在所有 C++ 提交中击败了50.00%的用户
11+
*/
12+
unordered_map<char, char> eq;
13+
14+
bool equationsPossible(vector<string>& equations) {
15+
int n = equations.size();
16+
17+
for(const string &s: equations) {
18+
bool flag1 = insert_node(eq, s[0]);
19+
bool flag2 = insert_node(eq, s[3]); // 插入
20+
if (s[1] == '=') join(eq, s[0], s[3]);
21+
}
22+
23+
for(const string &s: equations) {
24+
if (s[1] == '!') {
25+
char root1 = find(eq, s[0]);
26+
char root2 = find(eq, s[3]);
27+
if (root1 == root2) return false;
28+
}
29+
}
30+
31+
return true;
32+
}
33+
34+
bool is_exist(unordered_map<char, char>& set, char x) {
35+
return set.find(x) != set.end();
36+
}
37+
bool insert_node(unordered_map<char, char>& set, char x) {
38+
if (is_exist(set, x)) return false;
39+
set[x] = x;
40+
return true;
41+
}
42+
char find(unordered_map<char, char>& set, char x) {
43+
if (set[x] == x) return x;
44+
return find(set, set[x]);
45+
}
46+
void join(unordered_map<char, char>& set, char x, char y) {
47+
char x_root = find(set, x);
48+
char y_root = find(set, y);
49+
set[y_root] = x_root;
50+
}
51+
};

‎cpp/990_1.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <vector>
2+
#include <string>
3+
#include <unordered_map>
4+
using namespace std;
5+
6+
class Solution {
7+
public:
8+
/**
9+
* 提示:
10+
* 1. 1 <= equations.length <= 500
11+
* 2. equations[i].length == 4
12+
* 3. equations[i][0] 和 equations[i][3] 是小写字母
13+
* 4. equations[i][1] 要么是 '=',要么是 '!'
14+
* 5. equations[i][2] 是 '='
15+
*/
16+
/**
17+
* 改进,使用数组代替之前用的hashmap, 增加并查集的路径压缩算法、。
18+
*
19+
* 改进后:
20+
* 速度: 4 ms, 在所有 C++ 提交中击败了98.83%的用户。
21+
* 内存: 内存消耗 :10.8 MB, 在所有 C++ 提交中击败了50.00%的用户。
22+
*/
23+
24+
bool equationsPossible(vector<string>& equations) {
25+
// 并查集初始化
26+
int set[30];
27+
for (int i = 0; i < 30; ++i) set[i] = i;
28+
for (const string &s : equations) {
29+
if (s[1] == '=') {
30+
join(set, s[0]-'a', s[3]-'a');
31+
}
32+
}
33+
for (const string &s : equations) {
34+
if (s[1] == '!') {
35+
if (find(set, s[0]-'a') == find(set, s[3]-'a'))
36+
return false;
37+
}
38+
}
39+
return true;
40+
}
41+
42+
43+
char find(int *set, int x) {
44+
if (set[x] == x) return x;
45+
int res = find(set, set[x]); // 路径压缩算法
46+
return res;
47+
}
48+
void join(int *set, int x, int y) {
49+
int root_x = find(set, x);
50+
int root_y = find(set, y);
51+
set[root_y] = root_x;
52+
}
53+
};

‎python/46.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution:
2+
def translateNum(self, num: int) -> int:
3+
# dfs
4+
# 12258
5+
num_str = str(num)
6+
cache = {} # 缓存, 空间换时间 40ms->36ms
7+
# count = 0
8+
def _dfs(num_str):
9+
# nonlocal count
10+
if not len(num_str):
11+
# count+=1
12+
return 1
13+
14+
num_str1 = num_str[1:]
15+
num_str2 = num_str[2:]
16+
17+
if not num_str1 in cache:
18+
res1 = _dfs(num_str1)
19+
cache[num_str1] = res1
20+
else:
21+
res1 = cache[num_str1]
22+
23+
res2 = 0
24+
num_2 = int(num_str[:2])
25+
if len(num_str) >= 2 and num_2 <26 and num_2 > 9:
26+
if num_str2 in cache:
27+
res2 += _dfs(num_str2)
28+
cache[num_2] = res2
29+
else:
30+
res2 = cache[num_str2]
31+
32+
return res1 + res2
33+
34+
# print(count)
35+
return _dfs(num_str)

‎python/46_dp.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def translateNum(self, num: int) -> int:
3+
str_num = str(num)
4+
n = len(num)
5+
dp = [0] * n + 1 # 前n个数有多少种可能
6+
dp[0] = 1
7+
dp[1] = 1
8+
for i in range(2, n+1):
9+
temp = int(str_num[i-2:i])
10+
if temp > 9 and temp < 26:
11+
dp[i] += dp[i-2]
12+
dp[i] += dp[i-1]
13+
14+
return dp[n]
15+
16+
def translateNum2(self, num: int) -> int:
17+
n = len(num)
18+
str_num = str(num)
19+
a, b = 1, 1 # 由于只用到后面两个数,所有可以直接迭代, 节约了空间
20+
for i in range(2, n+1):
21+
temp = int(str_num[i-2:i])
22+
if temp > 9 and temp < 26:
23+
a, b = a+b, a
24+
else:
25+
a, b = a, a
26+
return a

0 commit comments

Comments
 (0)
Please sign in to comment.