File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -59,6 +59,33 @@ class Solution:
59
59
return len (A)
60
60
```
61
61
62
+ ### C++
63
+ ``` c++
64
+ class Solution {
65
+ /**
66
+ * param A : an integer sorted array
67
+ * param target : an integer to be inserted
68
+ * return : an integer
69
+ * /
70
+ public:
71
+ int searchInsert(vector<int > &A, int target) {
72
+ // write your code here
73
+ if (A.empty()) return 0;
74
+
75
+ int n = A.size();
76
+ int lb = -1, ub = n;
77
+ while (lb + 1 < ub) {
78
+ int mid = lb + (ub - lb) / 2;
79
+ if (A[ mid] < target) {
80
+ lb = mid;
81
+ } else {
82
+ ub = mid;
83
+ }
84
+ }
85
+ return ub;
86
+ }
87
+ };
88
+ ```
62
89
63
90
### Java
64
91
仍然是 [Binary Search](http://algorithm.yuanbin.me/zh-cn/basics_algorithm/binary_search.html) 中`lower_bound`的变形,两大关键点:`start` 和`end` 的初始化;最终插入位置和`start` 以及`end` 之间的关系,由于`start`对应的索引一定是小于目标值的,那么`start + 1` 就是要求的值了,再检查下两端的边界,DONE
You can’t perform that action at this time.
0 commit comments