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 21e5f8e

Browse files
哈希表增加部分题目
1 parent ae17a4d commit 21e5f8e

6 files changed

+190
-0
lines changed

‎哈希表/299.猜数字游戏.cpp‎

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=299 lang=cpp
3+
*
4+
* [299] 猜数字游戏
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
string getHint(string secret, string guess) {
11+
// 数字对了 - 数字和位置全对了
12+
// 1. 数字 位置全对了
13+
int count1 = 0;
14+
// 2. 数字对了,不论顺序
15+
int count2 = 0;
16+
vector<int> m1(10);
17+
vector<int> m2(10);
18+
int n = secret.size();
19+
for (int i = 0; i < n; i++) {
20+
if (secret[i] == guess[i])
21+
{
22+
count1++;
23+
}
24+
m1[secret[i] - '0']++;
25+
m2[guess[i] - '0']++;
26+
}
27+
for (size_t i = 0; i < 10; i++)
28+
{
29+
count2 += min(m1[i], m2[i]);
30+
}
31+
stringstream oss;
32+
oss << count1 << "A" << count2 - count1 << "B";
33+
return oss.str();
34+
}
35+
};
36+
// @lc code=end
37+
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=347 lang=cpp
3+
*
4+
* [347] 前 K 个高频元素
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
static bool cmp(const pair<int, int>& a, const pair<int, int>& b) {
11+
return a.second > b.second;
12+
}
13+
14+
vector<int> topKFrequent(vector<int>& nums, int k) {
15+
// 记录num的出现次数
16+
unordered_map<int, int> counting;
17+
for (int a : nums) {
18+
counting[a]++;
19+
}
20+
// 小顶堆,前 k 个频次最高的num
21+
// &Solution::cmp 类型为 void (*ptr)(const pair<int, int>&, const pair<int, int>&)
22+
// decltype(&Solution::cmp) 获取Solution::cmp函数的声明类型
23+
priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(&Solution::cmp)> heap(Solution::cmp);
24+
for (auto& kv : counting) {
25+
if (heap.size() < k)
26+
heap.push(kv);
27+
else if (heap.size() >= k && kv.second >= heap.top().second) {
28+
heap.push(kv);
29+
heap.pop();
30+
}
31+
}
32+
33+
vector<int> ans;
34+
for (int i = 0; i < k; i++) {
35+
ans.push_back(heap.top().first);
36+
heap.pop();
37+
}
38+
return ans;
39+
}
40+
41+
};
42+
// @lc code=end
43+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* @lc app=leetcode.cn id=387 lang=cpp
3+
*
4+
* [387] 字符串中的第一个唯一字符
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
int firstUniqChar(string s) {
11+
vector<int> memo(255);
12+
for (char c : s) {
13+
memo[c]++;
14+
}
15+
for (int i = 0; i < s.size(); i++) {
16+
if (memo[s[i]] == 1) {
17+
return i;
18+
}
19+
}
20+
return -1;
21+
}
22+
};
23+
// @lc code=end
24+

‎哈希表/389.找不同.cpp‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* @lc app=leetcode.cn id=389 lang=cpp
3+
*
4+
* [389] 找不同
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
char findTheDifference(string s, string t) {
11+
char ans = 0;
12+
for (char c : s) {
13+
ans = ans ^ c;
14+
}
15+
for (char c : t) {
16+
ans = ans ^ c;
17+
}
18+
return ans;
19+
}
20+
};
21+
// @lc code=end
22+

‎哈希表/409.最长回文串.cpp‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* @lc app=leetcode.cn id=409 lang=cpp
3+
*
4+
* [409] 最长回文串
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
int longestPalindrome(string s) {
11+
// 双数一定能构造
12+
// 只能有一个单数
13+
int odd = 0;
14+
int ans = 0;
15+
unordered_map<char, int> memo;
16+
for (char c: s) {
17+
memo[c]++;
18+
}
19+
for (auto& kv : memo) {
20+
if (kv.second % 2 == 0) {
21+
ans += kv.second;
22+
} else {
23+
odd = 1;
24+
ans += kv.second - 1;
25+
}
26+
}
27+
return ans + odd;
28+
}
29+
};
30+
// @lc code=end
31+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* @lc app=leetcode.cn id=438 lang=cpp
3+
*
4+
* [438] 找到字符串中所有字母异位词
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
vector<int> findAnagrams(string s, string p) {
11+
if (s.size() < p.size()) return {};
12+
int k = p.size();
13+
vector<int> mp(256);
14+
vector<int> ms(256);
15+
for (int i = 0; i < k; i++) {
16+
mp[p[i]]++;
17+
ms[s[i]]++;
18+
}
19+
vector<int> ans;
20+
if (mp == ms) ans.push_back(0);
21+
// 滑动窗口
22+
for (int i = k; i < s.size(); i++) {
23+
// 丢弃一个
24+
ms[s[i-k]]--;
25+
// 加入一个
26+
ms[s[i]]++;
27+
if (mp == ms) ans.push_back(i-k+1);
28+
}
29+
return ans;
30+
}
31+
};
32+
// @lc code=end
33+

0 commit comments

Comments
(0)

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