Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ab4ad45

Browse files
Bruce YangBruce Yang
Bruce Yang
authored and
Bruce Yang
committed
Merge branch 'master' of github.com:yanglr/leetcode-ac
2 parents 92c1af9 + 08b7fac commit ab4ad45

File tree

132 files changed

+5947
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+5947
-1
lines changed

‎algo-basic/acwing167-sticks.cpp

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include <algorithm>
2+
// #include <cstring>
3+
#include <iostream>
4+
using namespace std;
5+
6+
const int N = 70;
7+
int n; // 所有木棒的个数
8+
int sum, length; /* sum: 所有木棒的长度总和, length: 当前枚举的木棍的长度 */
9+
10+
int sticks[N]; // 存所有的木棒
11+
bool st[N]; // 存每根木棒当前有没有被用过
12+
13+
/**
14+
* u: 当前拼到了第几根(1, 2...)木棍, 木棒内部的编号No.
15+
cur: 当前这根木棍已经拼了多长
16+
start: 当前从原木棒数组的哪个index开始枚举
17+
*/
18+
bool dfs(int u, int cur, int start)
19+
{
20+
if (u * length == sum) // 找到了一组解
21+
{
22+
return true;
23+
}
24+
if (cur == length) /* 搜索下一个新的木棍 */
25+
return dfs(u + 1, 0, 0);
26+
for (int i = start; i < n; i++) // 循环需要从start开始
27+
{
28+
if (st[i]) continue; // 当前木棍用掉了
29+
int l = sticks[i];
30+
if (cur + l <= length)
31+
{
32+
st[i] = true;
33+
if (dfs(u, cur + l, i + 1))
34+
{
35+
return true;
36+
}
37+
st[i] = false; // 清空访问状态
38+
// 剪枝3 如果是第一根木棍放入失败, 则一定失败
39+
if (!cur)
40+
{
41+
return false;
42+
}
43+
//剪枝4 如果是最后一根木棒放入失败, 则一定失败
44+
if (cur + l == length)
45+
{
46+
return false;
47+
}
48+
// 剪枝2 如果失败了, 则跳过长度相等的木棒
49+
int j = i;
50+
while (j < n && sticks[j] == l)
51+
{
52+
j++;
53+
}
54+
i = j - 1;
55+
}
56+
}
57+
return false;
58+
}
59+
60+
/**
61+
输入样例:
62+
63+
9
64+
5 2 1 5 2 1 5 2 1
65+
4
66+
1 2 3 4
67+
0
68+
*/
69+
// Test
70+
int main()
71+
{
72+
while (cin >> n, n)
73+
{
74+
sum = 0, length = 0;
75+
fill(st, st + N, false); // 把所有长度>50的木棍的访问状态清空一下
76+
for (int i = 0; i < n; i++)
77+
{
78+
cin >> sticks[i];
79+
if (sticks[i] > 50)
80+
{
81+
continue;
82+
}
83+
sum += sticks[i];
84+
length = max(length, sticks[i]);
85+
}
86+
87+
// 剪枝: 优化搜索顺序
88+
sort(sticks, sticks + n, greater<int>());
89+
for (int i = 0; i < n; i++)
90+
{
91+
if (sticks[i] > 50)
92+
{
93+
st[i] = true;
94+
}
95+
}
96+
97+
while (true)
98+
{
99+
if (sum % length == 0 && dfs(0, 0, 0))
100+
{
101+
cout << length << endl;
102+
break;
103+
}
104+
length++;
105+
}
106+
}
107+
return 0;
108+
}

‎algo-basic/in167.txt

Whitespace-only changes.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include<vector>
2+
#include<algorithm>
3+
#include<iostream>
4+
#include<unordered_map>
5+
using namespace std;
6+
7+
class Solution {
8+
private:
9+
struct Node {
10+
int idx;
11+
vector<Node*> children;
12+
Node() : idx(-1), children(26, nullptr) {}
13+
};
14+
struct Trie {
15+
Node* root;
16+
Trie() : root(new Node()) {}
17+
void insert(string& word, int idx)
18+
{
19+
Node* p = root;
20+
for (char& c : word) {
21+
c -= 'a';
22+
if (p->children[c] == nullptr)
23+
{
24+
p->children[c] = new Node();
25+
}
26+
p = p->children[c];
27+
}
28+
p->idx = idx;
29+
}
30+
};
31+
public:
32+
vector<vector<int>> multiSearch(string big, vector<string>& smalls) {
33+
unordered_map<string, vector<int>> cache;
34+
const int n = big.size();
35+
const int m = smalls.size();
36+
vector<vector<int>> res(m);
37+
38+
Trie trie = Trie();
39+
// 构造前缀树
40+
for (int i = 0; i < m; i++)
41+
{
42+
trie.insert(smalls[i], i);
43+
}
44+
for (int i = 0; i < n; i++)
45+
{
46+
int j = i;
47+
Node* node = trie.root;
48+
while (j < n && node->children[big[j] - 'a'])
49+
{
50+
node = node->children[big[j] - 'a'];
51+
if (node->idx != -1)
52+
{
53+
res[node->idx].push_back(i);
54+
}
55+
j++;
56+
}
57+
}
58+
return res;
59+
}
60+
};
61+
62+
// Test
63+
int main()
64+
{
65+
Solution sol;
66+
string big = "mississippi";
67+
vector<string> smalls = {"is", "ppi", "hi", "sis", "i", "ssippi"};
68+
auto res = sol.multiSearch(big, smalls);
69+
for (auto& row : res) // 遍历每一行
70+
{
71+
for (auto& elem : row) // 输出每一个元素
72+
cout << elem << " ";
73+
cout << "\n";
74+
}
75+
76+
return 0;
77+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include<vector>
2+
#include<algorithm>
3+
#include<iostream>
4+
#include<cstring>
5+
using namespace std;
6+
7+
class Solution {
8+
public:
9+
vector<string> commonChars(vector<string>& words) {
10+
vector<string> res;
11+
int minDict[26]; // 统计每个字母的最小count
12+
fill(minDict, minDict + 26, INT_MAX);
13+
for (int j = 0; j < words.size(); j++) // 将每一个单词进行哈希化, 然后分别算出每个字母的最小count
14+
{
15+
string curWord = words[j];
16+
vector<int> curDict(26, 0); // 26个字母
17+
for (auto& ch : curWord)
18+
curDict[ch - 'a']++;
19+
for (int i = 0; i < 26; i++)
20+
minDict[i] = min(minDict[i], curDict[i]);
21+
}
22+
for (int i = 0; i < 26; i++)
23+
{
24+
int curCount = minDict[i];
25+
if (curCount >= 1)
26+
{
27+
auto ch = i + 'a';
28+
string str;
29+
str.push_back(ch);
30+
for (int i = 0; i < curCount; i++)
31+
res.push_back(str);
32+
}
33+
}
34+
return res;
35+
}
36+
};
37+
38+
// Test
39+
int main()
40+
{
41+
Solution sol;
42+
vector<string> words = {"cool", "lock", "cook"};
43+
auto res = sol.commonChars(words);
44+
for (auto str : res)
45+
cout << str << endl;
46+
47+
return 0;
48+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include<vector>
2+
#include<algorithm>
3+
#include<iostream>
4+
using namespace std;
5+
6+
class Solution {
7+
public:
8+
bool isValid(string s) {
9+
string stk; // 用string模拟栈
10+
for (auto& c: s) {
11+
stk += c;
12+
if (stk.size() >= 3 && stk.substr(stk.size() - 3) == "abc")
13+
{
14+
for (int i = 0; i < 3; i++)
15+
stk.pop_back();
16+
}
17+
}
18+
return stk.empty();
19+
}
20+
};
21+
22+
// Test
23+
int main()
24+
{
25+
Solution sol;
26+
string s = "abcabcababcc";
27+
auto res = sol.isValid(s);
28+
cout << (res ? "True" : "False") << endl;
29+
30+
return 0;
31+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include<vector>
2+
#include<algorithm>
3+
#include<iostream>
4+
using namespace std;
5+
6+
class Solution {
7+
public:
8+
int longestOnes(vector<int>& nums, int k) {
9+
const int N = nums.size();
10+
int i = 0; // i: 滑动窗口左边界, 固定右边界, 移动左边界
11+
int count = 0; // 已经使用了多少次反转次数
12+
int res = 0; // res: max len
13+
for (int j = 0; j < N; j++)
14+
{
15+
if (nums[j] == 1) // 新加入窗口右侧的数是1, 不需要使用反转次数
16+
res = max(res, j - i + 1);
17+
else {
18+
count++;
19+
while (count > k) // 此时使用的反转次数超了, 需要移动左边界, 直到窗口使用的反转次数减小回到k为止
20+
{
21+
if (nums[i] == 0)
22+
count--;
23+
i++;
24+
}
25+
res = max(res, j - i + 1);
26+
}
27+
}
28+
return res;
29+
}
30+
};
31+
32+
// Test
33+
int main()
34+
{
35+
Solution sol;
36+
vector<int> nums = {1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0};
37+
int k = 2;
38+
auto res = sol.longestOnes(nums, k);
39+
cout << res << endl;
40+
41+
return 0;
42+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include<vector>
2+
#include<algorithm>
3+
#include<iostream>
4+
using namespace std;
5+
6+
class Solution {
7+
public:
8+
int longestOnes(vector<int>& nums, int k) {
9+
const int N = nums.size();
10+
int left = 0; // left: 滑动窗口左边界, 固定右边界, 移动左边界
11+
int j = 0; // j: 滑动窗口右边界right
12+
int preSum = 0; // 使用的反转次数 0 -> 1
13+
int res = 0;
14+
for (int j = 0; j < N; j++)
15+
{
16+
preSum += (nums[j] == 0 ? 1 : 0);
17+
if (preSum > k) // 一开始, 让反转次数达到k+1, 然后开始根据情况收缩左边界
18+
{
19+
while (left < j && nums[left] == 1) /* 在左边界每遇到一个1, 就直接移动左边界, 如果遇到1个0, 翻转次数又新腾出了1, 需要先更新preSum的值 */
20+
left++;
21+
22+
preSum -= 1;
23+
left++;
24+
}
25+
res = max(res, j - left + 1); // 此时, 反转次数恰好恢复到k, 可以更新结果了
26+
}
27+
return res;
28+
}
29+
};
30+
31+
// Test
32+
int main()
33+
{
34+
Solution sol;
35+
vector<int> nums = {1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0};
36+
int k = 2;
37+
auto res = sol.longestOnes(nums, k);
38+
cout << res << endl;
39+
40+
return 0;
41+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /