File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1717class Solution {
1818public:
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
Original file line number Diff line number Diff line change 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+
Original file line number Diff line number Diff line change 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+
You can’t perform that action at this time.
0 commit comments