# Leetcode 35. Search Insert Position ###### tags: `Leetcode(C++)` 題目 : https://leetcode.com/problems/search-insert-position/ 。 想法 : 二分搜,最後一個數要比較在左邊還在右邊。 時間複雜度 : O(logn)。 程式碼 : ``` class Solution { public: int searchInsert(vector<int>& nums, int target) { int l = 0, r = nums.size() - 1; while(l != r){ int mid = (l + r) / 2; if(nums[mid] >= target) r = mid; else l = mid + 1; } if(target > nums[l]) return l+1; return l; } }; ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up