# 演算法題 - Binary Search - Leetcode - by PeterWang ## 題目資訊 Leetcode 704 ###### tags: `Binary Search` `Leetcode` ## Binary Search概述 以下面程式碼為例,把搜尋的範圍不斷縮小,可以減少搜尋時間。 ## 程式碼 ```clike= class Solution { public: int search(vector<int>& nums, int target) { int maxn=nums.size()-1; int minn=0; int mid=(maxn+minn)/2; while(maxn!=minn){ mid=(maxn+minn)/2; if(nums[mid]>=target){ maxn=mid; } else{ minn=mid+1; } } if(nums[maxn]==target){ return maxn; } else{ return -1; } } }; ``` ## 資料來源 [leetcode](https://leetcode.com/) [題目敘述](https://leetcode.com/problems/binary-search/) ## 備註 >[name=PeterWang] >[time=Mon, Jun 14, 2021 11:29 AM]
×
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