int main(){ vector<int> A = {1, 3, 5, 6}; cout<<searchInsert(A, 5)<<endl; cout<<searchInsert(A, 2)<<endl; cout<<searchInsert(A, 7)<<endl; cout<<searchInsert(A, 0)<<endl; return 0; }
int searchInsert(const vector<int>& nums, int begin, int end, int target) { if (end - begin < 1) { return begin; } if (end - begin == 1) { return nums[begin] >= target ? begin : begin + 1; } int mid = (begin + end) / 2; if (target == nums[mid]) { return mid; } else if (target < nums[mid]) { return searchInsert(nums, begin, mid, target); } else { return searchInsert(nums, mid, end, target); } }
int main() { { int nums[] = { 1 }; int target = 1; printf("%d\n", searchInsert(nums, sizeof(nums) / sizeof(int), target)); } system("PAUSE"); return 0; }
int main(){ int n,i,tmp,target; scanf("%d",&n); int A[200]={0}; for(i=0;i<n;i++) scanf("%d",&A[i]); scanf("%d",&target); printf("%d",searchInsert(A,n,target)); return 0; }
int main() { int A[]={1,3,5,6}; int len = sizeof(A)/sizeof(A[0]); int target = 0; printf("请输入target:\n"); scanf("%d",&target); printf("\nindex=%d\n",searchInsert(A,len,target)); fflush(stdin); getchar(); return 0; }
int main(){ int A[] = {1, 2, 3, 4,5 , 10}; int size = sizeof(A)/sizeof(A[0]); cout<<searchInsert(A,size,2)<<endl; // int A[] = {1, 3, 5, 6}; // int size = sizeof(A)/sizeof(A[0]); // cout<<searchInsert(A,size,5)<<endl; // cout<<searchInsert(A,size,2)<<endl; // cout<<searchInsert(A,size,7)<<endl; // cout<<searchInsert(A,size,0)<<endl; return 0; }
int LIS(vector<int>& nums) { vector<int> v; v.push_back(INT_MIN); for(int num : nums) { if(num > v[v.size()-1]) v.push_back(num); else { int index = searchInsert(v, num); v[index] = num; } } return v.size() - 1; }
int main() { int set[] = {1, 3, 5, 7, 9}; int setsize = sizeof(set)/sizeof(int); int target; int res1 = -1; int res2 = -1; for(target = 0; target <= 10; target+=2) { res1 = searchInsert(set, setsize, target); res2 = searchInsert2(set, setsize, target); printf("for target %d\n", target); printf("res1 is %d\n", res1); printf("res2 is %d\n\n", res2); } }
int searchInsert(vector<int>& nums, int target) { return searchInsert(nums, 0, nums.size(), target); }