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 4ca9df3

Browse files
新增部分哈希表题目
1 parent 21e5f8e commit 4ca9df3

8 files changed

+279
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @lc app=leetcode.cn id=451 lang=cpp
3+
*
4+
* [451] 根据字符出现频率排序
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
string frequencySort(string s) {
11+
vector<int> counting(256);
12+
for (char c : s) {
13+
counting[c]++;
14+
}
15+
string ans;
16+
while (ans.size() < s.size()) {
17+
int max_idx = 0;
18+
for (size_t i = 0; i < counting.size(); i++) {
19+
if (counting[i] > counting[max_idx]) {
20+
max_idx = i;
21+
}
22+
}
23+
ans += string(counting[max_idx], max_idx);
24+
counting[max_idx] = 0;
25+
}
26+
return ans;
27+
}
28+
};
29+
// @lc code=end
30+

‎哈希表/500.键盘行.cpp‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* @lc app=leetcode.cn id=500 lang=cpp
3+
*
4+
* [500] 键盘行
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
vector<string> findWords(vector<string>& words) {
11+
vector<unordered_set<char>> ss(3);
12+
vector<string> tmp = { "qwertyuiop", "asdfghjkl", "zxcvbnm" };
13+
for (size_t i = 0; i < tmp.size(); i++) {
14+
for (char c : tmp[i]) {
15+
ss[i].insert(c);
16+
ss[i].insert(c & '_'); // 转成大写
17+
}
18+
}
19+
vector<string> ans;
20+
for (const string& w : words) {
21+
int idx = -1;
22+
for (size_t i = 0; i < ss.size(); i++) {
23+
if (ss[i].find(w[0]) != ss[i].end()) {
24+
idx = i;
25+
break;
26+
}
27+
}
28+
bool flag = true;
29+
for (size_t j = 1; j < w.size(); j++) {
30+
if (ss[idx].find(w[j]) == ss[idx].end()) {
31+
flag = false;
32+
break;
33+
}
34+
}
35+
if (flag) {
36+
ans.push_back(w);
37+
}
38+
}
39+
return ans;
40+
}
41+
};
42+
// @lc code=end
43+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* @lc app=leetcode.cn id=508 lang=cpp
3+
*
4+
* [508] 出现次数最多的子树元素和
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* struct TreeNode {
11+
* int val;
12+
* TreeNode *left;
13+
* TreeNode *right;
14+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
15+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
16+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
17+
* };
18+
*/
19+
class Solution {
20+
public:
21+
vector<int> findFrequentTreeSum(TreeNode* root) {
22+
int _ = getSum(root);
23+
int max_count = -1;
24+
for (auto& kv : memo) {
25+
max_count = max(max_count, kv.second);
26+
}
27+
vector<int> ans;
28+
for (auto& kv : memo) {
29+
if (kv.second == max_count) {
30+
ans.push_back(kv.first);
31+
}
32+
}
33+
return ans;
34+
}
35+
36+
int getSum(TreeNode* root) {
37+
if (root == nullptr)
38+
{
39+
return 0;
40+
}
41+
int sum = root->val + getSum(root->left) + getSum(root->right);
42+
memo[sum]++;
43+
return sum;
44+
}
45+
46+
private:
47+
unordered_map<int, int> memo;
48+
};
49+
// @lc code=end
50+

‎哈希表/525.连续数组.cpp‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* @lc app=leetcode.cn id=525 lang=cpp
3+
*
4+
* [525] 连续数组
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
int findMaxLength(vector<int>& nums) {
11+
// 前缀和 + 哈希表
12+
// 将 0 变为 -1
13+
// 1. 若前缀和为0,其长度为 i + 1
14+
// 2. 若前缀和nums[i] == nums[j], 则 [i+1, j]的元素 01个数相等, 长度为 j - i
15+
if (nums.size() < 2) {
16+
return 0;
17+
}
18+
int ans = 0;
19+
unordered_map<int, int> memo;
20+
nums[0] = nums[0] == 0 ? -1 : nums[0];
21+
memo[nums[0]] = 0;
22+
for (int i = 1; i < nums.size(); i++) {
23+
nums[i] = nums[i] == 0 ? -1 : nums[i];
24+
nums[i] = nums[i] + nums[i-1];
25+
if (memo.find(nums[i]) == memo.end()) {
26+
memo[nums[i]] = i;
27+
} else {
28+
ans = max(ans, i - memo[nums[i]]);
29+
}
30+
if (nums[i] == 0) {
31+
ans = max(ans, i+1);
32+
}
33+
}
34+
35+
return ans;
36+
// 0 0 0 1 1 0 0 1 1 1
37+
// -1 -2 -3 -2 -1 -2 -3 -2 -1 0
38+
}
39+
};
40+
// @lc code=end
41+

‎哈希表/554.砖墙.cpp‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @lc app=leetcode.cn id=554 lang=cpp
3+
*
4+
* [554] 砖墙
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
int leastBricks(vector<vector<int>>& wall) {
11+
// 统计边界的最大个数
12+
unordered_map<int, int> bound;
13+
for (const vector<int>& v : wall)
14+
{
15+
int x = 0;
16+
for (int i = 0; i < v.size() - 1; i++) {
17+
x += v[i];
18+
bound[x]++;
19+
}
20+
}
21+
int res = 0;
22+
for (auto& kv : bound) {
23+
res = max(res, kv.second);
24+
}
25+
return wall.size() - res;
26+
}
27+
};
28+
// @lc code=end
29+

‎哈希表/575.分糖果.cpp‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* @lc app=leetcode.cn id=575 lang=cpp
3+
*
4+
* [575] 分糖果
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
int distributeCandies(vector<int>& candyType) {
11+
unordered_set<int, int> memo;
12+
for (int c : candyType) {
13+
memo[c]++;
14+
}
15+
return min(candyType.size() / 2, memo.size());
16+
}
17+
};
18+
// @lc code=end
19+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @lc app=leetcode.cn id=594 lang=cpp
3+
*
4+
* [594] 最长和谐子序列
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
int findLHS(vector<int>& nums) {
11+
int ans = 0;
12+
unordered_map<int, int> memo; // 统计个数
13+
for (int i = 0; i < nums.size(); i++) {
14+
memo[nums[i]]++;
15+
}
16+
// 子序列只能包含 a, a+1 或 a-1, a
17+
for (auto& kv : memo) {
18+
int key = kv.first;
19+
// key+1 不存在于 memo时,
20+
// 使用 memo[key+1]这种写法会初始化 memo[key+1] = 0, 影响for循环对memo的遍历
21+
if (memo.find(key+1) != memo.end())
22+
ans = max(ans, memo[key] + memo[key+1]);
23+
if (memo.find(key-1) != memo.end())
24+
ans = max(ans, memo[key] + memo[key-1]);
25+
}
26+
return ans;
27+
}
28+
};
29+
// @lc code=end
30+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* @lc app=leetcode.cn id=599 lang=cpp
3+
*
4+
* [599] 两个列表的最小索引总和
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
11+
unordered_map<string, int> map1;
12+
unordered_map<string, int> map2;
13+
for (size_t i = 0; i < list1.size(); i++) {
14+
map1[list1[i]] = i;
15+
}
16+
for (size_t i = 0; i < list2.size(); i++) {
17+
map2[list2[i]] = i;
18+
}
19+
vector<string> ans;
20+
int sum = INT_MAX;
21+
for (auto& kv : map2) {
22+
string k = kv.first;
23+
if (map1.find(k) != map1.end()) {
24+
if (map1[k] + map2[k] < sum) {
25+
ans.clear();
26+
ans.push_back(k);
27+
sum = map1[k] + map2[k];
28+
} else if (map1[k] + map2[k] == sum) {
29+
ans.push_back(k);
30+
}
31+
}
32+
}
33+
return ans;
34+
}
35+
};
36+
// @lc code=end
37+

0 commit comments

Comments
(0)

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