File tree 2 files changed +58
-0
lines changed
2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments