Skip to content

Commit 443c6b8

Browse files
author
eupho
committed
12/14-15
1 parent 85fb4e4 commit 443c6b8

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

Diff for: 217-contains-duplicate.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
set<int> s;
12+
for (auto& n : nums) {
13+
if (s.find(n) != s.end())
14+
return true;
15+
else {
16+
s.insert(n);
17+
}
18+
}
19+
return false;
20+
}
21+
};
22+
// @lc code=end

Diff for: 274-h-index.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
// nums[n - x] >= h
12+
// x <= h
13+
// nums[n - x - 1] <= h
14+
// mid = n - h;
15+
// h = n - mid;
16+
if (citations.empty()) return 0;
17+
if (citations.size() == 1) {
18+
return citations[0] >= 1;
19+
}
20+
sort(citations.begin(), citations.end());
21+
int n = citations.size(), right = n - 1, left = 0;
22+
int mid = 0, h = 0;
23+
while (left <= right) {
24+
mid = (left + right) >> 1;
25+
26+
if (citations[mid] >= n - mid) {
27+
h = n - mid;
28+
right = mid - 1;
29+
} else {
30+
left = mid + 1;
31+
}
32+
}
33+
return h;
34+
}
35+
};
36+
// @lc code=end

0 commit comments

Comments
 (0)