Skip to content

Commit 51e9d0d

Browse files
committed
更新双周赛13临时文件
1 parent 912071b commit 51e9d0d

File tree

6 files changed

+325
-8
lines changed

6 files changed

+325
-8
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "public.h"
2+
3+
//数学题
4+
5+
class Solution {
6+
public:
7+
string encode(int num) {
8+
int stlen = log(num + 1) / log(2);
9+
10+
int remain = num - (pow(2, stlen) - 1);
11+
12+
//求remain的stlen位二进制表示
13+
14+
string res;
15+
for (int i = 0; i < stlen; ++i)
16+
{
17+
res += "0";
18+
}
19+
20+
int precount = 0;
21+
while (remain > 0)
22+
{
23+
if (remain % 2)
24+
{
25+
res[precount] = '1';
26+
}
27+
remain /= 2;
28+
precount++;
29+
}
30+
reverse(res.begin(), res.end());
31+
return res;
32+
}
33+
};
34+
35+
/*
36+
int main()
37+
{
38+
Solution* s = new Solution();
39+
cout << s->encode(0) << endl;
40+
cout << s->encode(2) << endl;
41+
cout << s->encode(6) << endl;
42+
cout << s->encode(23) << endl;
43+
cout << s->encode(107) << endl;
44+
}
45+
*/
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include "public.h"
2+
3+
//
4+
//多叉树找两个节点的最近公共祖先
5+
6+
class Solution {
7+
public:
8+
string findSmallestRegion(vector<vector<string>>& regions, string region1, string region2) {
9+
unordered_map<string, string> rela;
10+
for (auto& r : regions)
11+
{
12+
for (int i = 1; i < r.size(); ++i)
13+
{
14+
rela[r[i]] = r[0];
15+
}
16+
}
17+
18+
//一起往上找
19+
20+
unordered_set<string> us;
21+
us.insert(region1);
22+
us.insert(region2);
23+
string pre1 = region1;
24+
string pre2 = region2;
25+
26+
while (true)
27+
{
28+
string par1 = "";
29+
if (rela.find(pre1) != rela.end())
30+
{
31+
par1 = rela[pre1];
32+
}
33+
string par2 = "";
34+
if (rela.find(pre2) != rela.end())
35+
{
36+
par2 = rela[pre2];
37+
}
38+
39+
if (par1 != "" && us.find(par1) != us.end())
40+
return par1;
41+
else
42+
{
43+
us.insert(par1);
44+
pre1 = par1;
45+
}
46+
47+
if (par2 != "" && us.find(par2) != us.end())
48+
return par2;
49+
else
50+
{
51+
us.insert(par2);
52+
pre2 = par2;
53+
}
54+
55+
}
56+
return "duMP";
57+
}
58+
};
59+
60+
/*
61+
int main()
62+
{
63+
Solution* s = new Solution();
64+
vector<vector<string>> regions = { {"Earth","North America","South America"},{"North America","United States","Canada"},{"United States","New York","Boston"},{"Canada","Ontario","Quebec"},{"South America","Brazil"} };
65+
cout << s->findSmallestRegion(regions, "Quebec", "New York");
66+
return 0;
67+
}
68+
*/
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
#include "public.h"
2+
3+
//
4+
5+
class Solution {
6+
private:
7+
unordered_map<int, int> um;
8+
9+
void init(vector<int>& vals)
10+
{
11+
for (auto& s : vals)
12+
{
13+
um[s] = s;
14+
}
15+
}
16+
17+
int GetFather(int x) {
18+
return (um[x] == x ? x : um[x] = GetFather(um[x]));
19+
}
20+
21+
void Merge(int x, int y) {
22+
int a = GetFather(x);
23+
int b = GetFather(y);
24+
if (a != b)
25+
{
26+
um[b] = a;
27+
}
28+
}
29+
30+
vector<string> split(string t)
31+
{
32+
string pre = "";
33+
vector<string> res;
34+
for (auto& ite : t)
35+
{
36+
if (ite == ' ')
37+
{
38+
res.push_back(pre);
39+
pre = "";
40+
}
41+
else pre.push_back(ite);
42+
}
43+
res.push_back(pre);
44+
return res;
45+
}
46+
47+
string strmerge(vector<string> input)
48+
{
49+
string pre;
50+
for (auto& ite : input)
51+
{
52+
pre += ite;
53+
pre += " ";
54+
}
55+
pre.pop_back();
56+
return pre;
57+
}
58+
59+
public:
60+
vector<string> generateSentences(vector<vector<string>>& synonyms, string text) {
61+
vector<int> vinit;
62+
63+
vector<string> textsp = split(text);
64+
65+
unordered_map<int, string> relation;
66+
unordered_map<string, int> relationrev;
67+
unordered_map<int, vector<string>> umgroup;
68+
int count = 0;
69+
for (auto& s : synonyms)
70+
{
71+
if (relationrev.find(s[0]) == relationrev.end())
72+
{
73+
vinit.push_back(count);
74+
relation[count] = s[0];
75+
relationrev[s[0]] = count;
76+
count++;
77+
}
78+
if (relationrev.find(s[1]) == relationrev.end())
79+
{
80+
vinit.push_back(count);
81+
relation[count] = s[1];
82+
relationrev[s[1]] = count;
83+
count++;
84+
}
85+
}
86+
for (auto& itertextsp : textsp)
87+
{
88+
if (relationrev.find(itertextsp) == relationrev.end())
89+
{
90+
vinit.push_back(count);
91+
relation[count] = itertextsp;
92+
relationrev[itertextsp] = count;
93+
count++;
94+
}
95+
}
96+
97+
98+
init(vinit);
99+
100+
for (auto& s : synonyms)
101+
{
102+
Merge(relationrev[s[0]], relationrev[s[1]]);
103+
}
104+
105+
//·Ö×é
106+
vector<vector<string>> vgroup;
107+
for (auto& id : vinit)
108+
{
109+
umgroup[GetFather(id)].push_back(relation[id]);
110+
}
111+
112+
for (auto& ium : umgroup)
113+
vgroup.push_back(ium.second);
114+
115+
//textתÊý×é
116+
vector<int> resArr;
117+
118+
for (auto& itertextsp : textsp)
119+
{
120+
resArr.push_back(GetFather(relationrev[itertextsp]));
121+
}
122+
123+
vector<vector<string>> resVstr = { textsp };
124+
125+
for (int i = 0; i < resArr.size(); ++i)
126+
{
127+
vector<vector<string>> tempStr;
128+
for (auto& itersub : umgroup[resArr[i]])
129+
{
130+
for (auto oneKindres : resVstr)
131+
{
132+
oneKindres[i] = itersub;
133+
tempStr.push_back(oneKindres);
134+
}
135+
}
136+
resVstr = tempStr;
137+
}
138+
139+
vector<string> sRES;
140+
for (auto& itt : resVstr)
141+
sRES.push_back(strmerge(itt));
142+
143+
sort(sRES.begin(), sRES.end());
144+
145+
return sRES;
146+
147+
}
148+
};
149+
150+
/*
151+
int main()
152+
{
153+
Solution* s = new Solution();
154+
vector<vector<string>> synonyms = { {"happy","joy"},{"sad","sorrow"},{"joy","cheerful"} };
155+
auto res = s->generateSentences(synonyms, "I am happy today but was sad yesterday");
156+
return 0;
157+
}
158+
*/
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "public.h"
2+
3+
//动态规划
4+
//对单独一个人操作
5+
6+
class Solution {
7+
public:
8+
int numberOfWays(int num_people) {
9+
vector<long long> memory(1);
10+
memory[0] = 1;
11+
12+
for (int i = 4; i <= num_people; i += 2)
13+
{
14+
long long pre = 0;
15+
//st = 0;
16+
for (int en = 1; en < i; en += 2)
17+
{
18+
if (en == 1 || en == i - 1)
19+
{
20+
pre += memory.back();
21+
}
22+
else
23+
{
24+
pre += memory[en / 2 - 1] * memory[(i - en) / 2 - 1];
25+
pre %= 1000000007;
26+
}
27+
}
28+
memory.push_back(pre % 1000000007);
29+
}
30+
return memory.back() % 1000000007;
31+
}
32+
};
33+
34+
/*
35+
int main()
36+
{
37+
Solution* s = new Solution();
38+
cout << s->numberOfWays(140) << endl;
39+
cout << s->numberOfWays(2) << endl;
40+
cout << s->numberOfWays(4) << endl;
41+
cout << s->numberOfWays(6) << endl;
42+
cout << s->numberOfWays(8) << endl;
43+
return 0;
44+
}
45+
46+
*/

cpp/leetcode/leetcode.vcxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,10 @@
413413
<ClCompile Include="470. implement-rand10-using-rand7.cpp" />
414414
<ClCompile Include="472. concatenated-words.cpp" />
415415
<ClCompile Include="473. Matchsticks to Square.cpp" />
416-
<ClCompile Include="5255. cells-with-odd-values-in-a-matrix.cpp" />
417-
<ClCompile Include="5256. reconstruct-a-2-row-binary-matrix.cpp" />
418-
<ClCompile Include="5257. number-of-closed-islands.cpp" />
419-
<ClCompile Include="5258. maximum-score-words-formed-by-letters.cpp" />
416+
<ClCompile Include="5108. encode-number.cpp" />
417+
<ClCompile Include="5109. smallest-common-region.cpp" />
418+
<ClCompile Include="5110. synonymous-sentences.cpp" />
419+
<ClCompile Include="5125. handshakes-that-dont-cross.cpp" />
420420
<ClCompile Include="摩尔投票法升级229. majority-element-ii.cpp" />
421421
<ClCompile Include="230. kth-smallest-element-in-a-bst.cpp" />
422422
<ClCompile Include="231. power-of-two.cpp" />

cpp/leetcode/leetcode.vcxproj.filters

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,16 +1166,16 @@
11661166
<ClCompile Include="472. concatenated-words.cpp">
11671167
<Filter>源文件\HardAlg\前缀树</Filter>
11681168
</ClCompile>
1169-
<ClCompile Include="5255. cells-with-odd-values-in-a-matrix.cpp">
1169+
<ClCompile Include="5108. encode-number.cpp">
11701170
<Filter>源文件</Filter>
11711171
</ClCompile>
1172-
<ClCompile Include="5256. reconstruct-a-2-row-binary-matrix.cpp">
1172+
<ClCompile Include="5109. smallest-common-region.cpp">
11731173
<Filter>源文件</Filter>
11741174
</ClCompile>
1175-
<ClCompile Include="5257. number-of-closed-islands.cpp">
1175+
<ClCompile Include="5110. synonymous-sentences.cpp">
11761176
<Filter>源文件</Filter>
11771177
</ClCompile>
1178-
<ClCompile Include="5258. maximum-score-words-formed-by-letters.cpp">
1178+
<ClCompile Include="5125. handshakes-that-dont-cross.cpp">
11791179
<Filter>源文件</Filter>
11801180
</ClCompile>
11811181
</ItemGroup>

0 commit comments

Comments
 (0)