Index: [Leetcode] leetcode key index (C++/JAVA/PYTHON/SQL)
Github:https://github.com/illuz/leetcode
035. Search Insert Position (Medium)
links:
Title: https://leetcode.com/problems/search-insert-position/
Code (GitHub): Https://github.com/illuz/leetcode
Test Instructions:
To insert a number into an ordered array, ask the insertion position.
Analysis:
Or a two-point deformation problem.
- With the STL
lower_bound lazy.
- Two points, finally pay attention to determine whether to find, what to output.
Code:
C++:
Class Solution {Public:int searchinsert (int a[], int n, int target) {//If use lower_bound//return Lower_bound (A, a + N, Target)-A;int L = 0, R = n-1, mid = 0;while (L < r) {mid = L + (r-l)/2;//mid = (L + R)/2;if (A[mid] < tar Get) L = mid + 1;elser = mid;} return a[l] < target? L + 1:l;}};
[Leetcode] 035. Search Insert Position (Medium) (c + +)