Skip to content

Commit f26940a

Browse files
committed
2020-09-28
1 parent 57d3337 commit f26940a

3 files changed

Lines changed: 70 additions & 1 deletion

File tree

700.二叉搜索树中的搜索.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,22 @@
1717
class Solution {
1818
public:
1919
TreeNode* searchBST(TreeNode* root, int val) {
20-
20+
if (!root) return NULL;
21+
stack<TreeNode * > st;
22+
st.push(root);
23+
while (st.size())
24+
{
25+
TreeNode * t = st.top();
26+
st.pop();
27+
if (t->val == val) return t;
28+
else
29+
{
30+
if (t->left) st.push(t->left);
31+
if (t->right) st.push(t->right);
32+
}
33+
34+
}
35+
return NULL;
2136
}
2237
};
2338
// @lc code=end
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=703 lang=cpp
3+
*
4+
* [703] 数据流中的第K大元素
5+
*/
6+
7+
// @lc code=start
8+
class KthLargest {
9+
public:
10+
// 使用一个 multiset
11+
multiset<int> st;
12+
int k;
13+
KthLargest(int k, vector<int>& nums) {
14+
this->k = k;
15+
st.insert(nums.begin(), nums.end());
16+
}
17+
int add(int val) {
18+
st.insert(val);
19+
while (st.size() > k) st.erase(st.begin());
20+
return *st.begin();
21+
}
22+
};
23+
24+
/**
25+
* Your KthLargest object will be instantiated and called as such:
26+
* KthLargest* obj = new KthLargest(k, nums);
27+
* int param_1 = obj->add(val);
28+
*/
29+
// @lc code=end
30+

704.二分查找.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=704 lang=cpp
3+
*
4+
* [704] 二分查找
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
int search(vector<int>& nums, int target) {
11+
int l = 0, r = nums.size();
12+
while (l < r)
13+
{
14+
int mid = l + r >> 1;
15+
if (nums[mid] < target) l = mid + 1;
16+
else r = mid;
17+
}
18+
if (l < 0 || l >= nums.size()) return -1;
19+
if (nums[l] != target) return -1;
20+
else return l;
21+
}
22+
};
23+
// @lc code=end
24+

0 commit comments

Comments
 (0)