File tree 7 files changed +206
-0
lines changed
7 files changed +206
-0
lines changed Original file line number Diff line number Diff line change @@ -140,6 +140,44 @@ var search = function (nums, target) {
140
140
};
141
141
```
142
142
143
+ ### ** C++**
144
+
145
+ 两遍二分,分别查找出左边界和右边界。
146
+
147
+ ``` cpp
148
+ class Solution {
149
+ public:
150
+ int search(vector<int >& nums, int target) {
151
+ int n = nums.size();
152
+ int left = 0, right = n;
153
+ int first, last;
154
+ while (left < right) {
155
+ int mid = left + (right - left) / 2;
156
+ if (nums[ mid] < target) {
157
+ left = mid + 1;
158
+ } else {
159
+ right = mid;
160
+ }
161
+ }
162
+ if (left == n || nums[ left] != target) {
163
+ return 0;
164
+ }
165
+ first = left;
166
+ left = 0, right = n;
167
+ while (left < right) {
168
+ int mid = left + (right - left) / 2;
169
+ if (nums[ mid] > target) {
170
+ right = mid;
171
+ } else {
172
+ left = mid + 1;
173
+ }
174
+ }
175
+ last = left - 1;
176
+ return last - first + 1;
177
+ }
178
+ };
179
+ ```
180
+
143
181
### **...**
144
182
145
183
```
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int search (vector<int >& nums, int target) {
4
+ int n = nums.size ();
5
+ int left = 0 , right = n;
6
+ int first, last;
7
+ while (left < right) {
8
+ int mid = left + (right - left) / 2 ;
9
+ if (nums[mid] < target) {
10
+ left = mid + 1 ;
11
+ } else {
12
+ right = mid;
13
+ }
14
+ }
15
+ if (left == n || nums[left] != target) {
16
+ return 0 ;
17
+ }
18
+ first = left;
19
+ left = 0 , right = n;
20
+ while (left < right) {
21
+ int mid = left + (right - left) / 2 ;
22
+ if (nums[mid] > target) {
23
+ right = mid;
24
+ } else {
25
+ left = mid + 1 ;
26
+ }
27
+ }
28
+ last = left - 1 ;
29
+ return last - first + 1 ;
30
+ }
31
+ };
Original file line number Diff line number Diff line change @@ -95,6 +95,26 @@ var missingNumber = function (nums) {
95
95
};
96
96
```
97
97
98
+ ### ** C++**
99
+
100
+ ``` cpp
101
+ class Solution {
102
+ public:
103
+ int missingNumber(vector<int >& nums) {
104
+ int left = 0, right = nums.size();
105
+ while (left < right) {
106
+ int mid = left + (right - left) / 2;
107
+ if (nums[ mid] == mid) {
108
+ left = mid + 1;
109
+ } else {
110
+ right = mid;
111
+ }
112
+ }
113
+ return left;
114
+ }
115
+ };
116
+ ```
117
+
98
118
### **...**
99
119
100
120
```
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int missingNumber (vector<int >& nums) {
4
+ int left = 0 , right = nums.size ();
5
+ while (left < right) {
6
+ int mid = left + (right - left) / 2 ;
7
+ if (nums[mid] == mid) {
8
+ left = mid + 1 ;
9
+ } else {
10
+ right = mid;
11
+ }
12
+ }
13
+ return left;
14
+ }
15
+ };
Original file line number Diff line number Diff line change @@ -131,6 +131,63 @@ var kthLargest = function (root, k) {
131
131
};
132
132
```
133
133
134
+ ### ** C++**
135
+
136
+ ``` cpp
137
+ class Solution {
138
+ public:
139
+ int kthLargest(TreeNode* root, int k) {
140
+ cur = k;
141
+ inOrder(root);
142
+ return res;
143
+ }
144
+
145
+ private:
146
+ int cur, res;
147
+
148
+ void inOrder(TreeNode* root) {
149
+ if (root) {
150
+ inOrder(root->right);
151
+ --cur;
152
+ if (cur == 0) {
153
+ res = root->val;
154
+ return;
155
+ }
156
+ inOrder (root->left);
157
+ }
158
+ }
159
+ };
160
+ ```
161
+
162
+ ### **Go**
163
+
164
+ 利用 Go 的特性,中序遍历“生产”的数字传到 `channel`,返回第 `k` 个。
165
+
166
+ ```go
167
+ func kthLargest(root *TreeNode, k int) int {
168
+ ch := make(chan int)
169
+ ctx, cancel := context.WithCancel(context.Background())
170
+ defer cancel()
171
+ go inOrder(ctx, root, ch)
172
+ for ; k > 1; k-- {
173
+ <-ch
174
+ }
175
+ return <-ch
176
+ }
177
+
178
+ func inOrder(ctx context.Context, cur *TreeNode, ch chan<- int) {
179
+ if cur != nil {
180
+ inOrder(ctx, cur.Right, ch)
181
+ select {
182
+ case ch <- cur.Val:
183
+ case <-ctx.Done():
184
+ return
185
+ }
186
+ inOrder(ctx, cur.Left, ch)
187
+ }
188
+ }
189
+ ```
190
+
134
191
### ** ...**
135
192
136
193
```
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int kthLargest (TreeNode* root, int k) {
4
+ cur = k;
5
+ inOrder (root);
6
+ return res;
7
+ }
8
+
9
+ private:
10
+ int cur, res;
11
+
12
+ void inOrder (TreeNode* root) {
13
+ if (root) {
14
+ inOrder (root->right );
15
+ --cur;
16
+ if (cur == 0 ) {
17
+ res = root->val ;
18
+ return ;
19
+ }
20
+ inOrder (root->left );
21
+ }
22
+ }
23
+ };
Original file line number Diff line number Diff line change
1
+ func kthLargest (root * TreeNode , k int ) int {
2
+ ch := make (chan int )
3
+ ctx , cancel := context .WithCancel (context .Background ())
4
+ defer cancel ()
5
+ go inOrder (ctx , root , ch )
6
+ for ; k > 1 ; k -- {
7
+ <- ch
8
+ }
9
+ return <- ch
10
+ }
11
+
12
+ func inOrder (ctx context.Context , cur * TreeNode , ch chan <- int ) {
13
+ if cur != nil {
14
+ inOrder (ctx , cur .Right , ch )
15
+ select {
16
+ case ch <- cur .Val :
17
+ case <- ctx .Done ():
18
+ return
19
+ }
20
+ inOrder (ctx , cur .Left , ch )
21
+ }
22
+ }
You can’t perform that action at this time.
0 commit comments