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 8cdc0a3

Browse files
添加部分哈希表题目
1 parent dde4f6f commit 8cdc0a3

10 files changed

+323
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* @lc app=leetcode.cn id=786 lang=cpp
3+
*
4+
* [786] 第 K 个最小的素数分数
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
// vector<int> kthSmallestPrimeFraction(vector<int>& arr, int k) {
11+
// int n = arr.size();
12+
// vector<vector<int>> vec;
13+
// for (int i = 0; i < n; i++) {
14+
// for (int j = i + 1; j < n; j++) {
15+
// vec.push_back({ arr[i], arr[j] });
16+
// }
17+
// }
18+
19+
// sort(vec.begin(), vec.end(), [](const vector<int>& a, const vector<int>& b) -> bool {
20+
// return a[0] * b[1] < b[0] * a[1];
21+
// });
22+
23+
// return vec[k-1];
24+
// }
25+
26+
vector<int> kthSmallestPrimeFraction(vector<int>& arr, int k) {
27+
int n = arr.size();
28+
double left = 0.0;
29+
double right = 1.0;
30+
while (left < right) {
31+
double mid = left + (right - left) / 2;
32+
vector<int> ans = { 0, 1 };
33+
// mid 左边有多少个
34+
int count = left_count(arr, mid, ans);
35+
if (count == k) // mid 左边正好有 k 个,即为ans
36+
return ans;
37+
else if (count < k)
38+
left = mid;
39+
else
40+
right = mid;
41+
}
42+
return { 0, 1 };
43+
}
44+
45+
46+
int left_count(const vector<int>& arr, double mid, vector<int>& bound) {
47+
int count = 0;
48+
// 两层for循环仍可以优化
49+
for (int j = 1; j < arr.size(); j++) {
50+
for (int i = 0; i < j; i++) {
51+
if ((double) arr[i] / arr[j] < mid) {
52+
count++;
53+
if (arr[i] * bound[1] > bound[0] * arr[j]) {
54+
bound[0] = arr[i];
55+
bound[1] = arr[j];
56+
}
57+
} else {
58+
break;
59+
}
60+
}
61+
}
62+
return count;
63+
}
64+
};
65+
// @lc code=end
66+

‎二分查找/910.最小差值-ii.cpp‎

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=910 lang=cpp
3+
*
4+
* [910] 最小差值 II
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
int smallestRangeII(vector<int>& nums, int k) {
11+
sort(nums.begin(), nums.end());
12+
int n = nums.size();
13+
int E = nums[n-1] - nums[0];
14+
// 从1到nums.size()的长度中任意一点都有可能是中间点
15+
for (int i = 1; i < n; i++) {
16+
int min_val = min(nums[0] + k, nums[i] - k);
17+
int max_val = max(nums[n-1] - k, nums[i-1] + k);
18+
E = min(E, max_val-min_val);
19+
}
20+
return E;
21+
}
22+
};
23+
// @lc code=end
24+

‎二分查找/912.排序数组.cpp‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* @lc app=leetcode.cn id=912 lang=cpp
3+
*
4+
* [912] 排序数组
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
vector<int> sortArray(vector<int>& nums) {
11+
sort(nums.begin(), nums.end());
12+
return nums;
13+
}
14+
};
15+
// @lc code=end
16+

‎哈希表/187.重复的dna序列.cpp‎

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=187 lang=cpp
3+
*
4+
* [187] 重复的DNA序列
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
vector<string> findRepeatedDnaSequences(string s) {
11+
if (s.size() <= 10) return { };
12+
unordered_set<string> memo;
13+
unordered_set<string> res;
14+
vector<string> ans;
15+
for (int i = 0; i < s.size() - 9; i++) {
16+
string a = s.substr(i, 10);
17+
if (memo.find(a) != memo.end()) {
18+
res.insert(a);
19+
}
20+
memo.insert(a);
21+
}
22+
23+
for (auto& item : res) {
24+
ans.push_back(item);
25+
}
26+
return ans;
27+
}
28+
};
29+
// @lc code=end
30+

‎哈希表/204.计数质数.cpp‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* @lc app=leetcode.cn id=204 lang=cpp
3+
*
4+
* [204] 计数质数
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
int countPrimes(int n) {
11+
vector<bool> isPrime(n, true);
12+
13+
int ans = 0;
14+
for (int i = 2; i < n; i++)
15+
{
16+
if (isPrime[i]) {
17+
ans++;
18+
// i 的倍数都不是质数
19+
int m = 2;
20+
while (m * i < n)
21+
{
22+
isPrime[m*i] = false;
23+
m++;
24+
}
25+
}
26+
}
27+
return ans;
28+
29+
}
30+
};
31+
// @lc code=end
32+

‎哈希表/205.同构字符串.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=205 lang=cpp
3+
*
4+
* [205] 同构字符串
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
bool isIsomorphic(string s, string t) {
11+
if (s.size() != t.size()) return false;
12+
int n = s.size();
13+
unordered_map<char, char> map;
14+
unordered_set<char> memo;
15+
for (int i = 0; i < n; i++) {
16+
if (map.find(s[i]) == map.end()) {
17+
// 已经被映射过了
18+
if (memo.find(t[i]) != memo.end())
19+
return false;
20+
map[s[i]] = t[i];
21+
memo.insert(t[i]);
22+
} else {
23+
if (map[s[i]] != t[i])
24+
return false;
25+
}
26+
}
27+
return true;
28+
}
29+
};
30+
// @lc code=end
31+

‎哈希表/217.存在重复元素.cpp‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* @lc app=leetcode.cn id=217 lang=cpp
3+
*
4+
* [217] 存在重复元素
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
bool containsDuplicate(vector<int>& nums) {
11+
unordered_set<int> st;
12+
for (int a : nums) {
13+
if (st.find(a) != st.end())
14+
return true;
15+
st.insert(a);
16+
}
17+
return false;
18+
}
19+
};
20+
// @lc code=end
21+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* @lc app=leetcode.cn id=219 lang=cpp
3+
*
4+
* [219] 存在重复元素 II
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
bool containsNearbyDuplicate(vector<int>& nums, int k) {
11+
unordered_map<int, int> map;
12+
for (int i = 0; i < nums.size(); i++) {
13+
if (map.find(nums[i]) != map.end() && abs(i - map[nums[i]]) <= k)
14+
return true;
15+
map[nums[i]] = i;
16+
}
17+
return false;
18+
}
19+
};
20+
// @lc code=end
21+

‎哈希表/274.h-指数.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=274 lang=cpp
3+
*
4+
* [274] H 指数
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
int hIndex(vector<int>& citations) {
11+
sort(citations.begin(), citations.end());
12+
int m = citations.size();
13+
int left = 0;
14+
int right = m - 1;
15+
while (left < right)
16+
{
17+
int mid = left + (right - left) / 2;
18+
if (citations[mid] >= (m - mid)) {
19+
right = mid;
20+
} else {
21+
left = mid + 1;
22+
}
23+
}
24+
if (citations[right] == 0) return 0; // testcase: [0]
25+
return m - right;
26+
}
27+
};
28+
// @lc code=end
29+

‎哈希表/290.单词规律.cpp‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* @lc app=leetcode.cn id=290 lang=cpp
3+
*
4+
* [290] 单词规律
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
bool wordPattern(string pattern, string s) {
11+
unordered_map<char, string> map;
12+
unordered_set<string> hasMap;
13+
vector<string> words;
14+
split(s, words);
15+
if (pattern.size() != words.size())
16+
return false;
17+
for (int i = 0; i < pattern.size(); i++) {
18+
char c = pattern[i];
19+
if (map.find(c) == map.end()) {
20+
// a 和 b 不能同时映射到同一个单词
21+
if (hasMap.find(words[i]) != hasMap.end()) {
22+
return false;
23+
}
24+
// cout << "c="<< c << "; wordds[i]=" << words[i] << endl;
25+
map[c] = words[i];
26+
hasMap.insert(words[i]);
27+
} else {
28+
if (map[c] != words[i]) {
29+
// cout << "c="<< c << "; wordds[i]=" << words[i] << endl;
30+
return false;
31+
}
32+
}
33+
}
34+
return true;
35+
}
36+
37+
void split(const string& s, vector<string>& words) {
38+
string str = "";
39+
for (char c : s) {
40+
if (c == ' ') {
41+
words.push_back(str);
42+
str = "";
43+
} else {
44+
str += c;
45+
}
46+
}
47+
if (str != "") {
48+
words.push_back(str);
49+
}
50+
}
51+
};
52+
// @lc code=end
53+

0 commit comments

Comments
(0)

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