Search Insert Position
生活随笔
收集整理的這篇文章主要介紹了
Search Insert Position
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目:
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Here are few examples.
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0
解析:
這題是為了實現std::lower_bound()
1 class Solution { 2 public: 3 int searchInsert(vector<int>& nums, int target) { 4 vector<int>::size_type i=0; 5 for(i;i < nums.size();i++) 6 { 7 if(target <= nums[i]) 8 return i; 9 } 10 return i; 11 } 12 };?
轉載于:https://www.cnblogs.com/raichen/p/4957958.html
總結
以上是生活随笔為你收集整理的Search Insert Position的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [LeetCode]Perfect Sq
- 下一篇: MaskedTextBox的聚焦和光标位