当前位置:网站首页>Force buckle 704 Binary search
Force buckle 704 Binary search
2022-06-30 04:56:00 【A wise man should not be bald】
class Solution {
public:
int search(vector<int>& nums, int target) {
// This dichotomy is to put target Put it in a left closed and right closed section , That is to say [left,right] in
// So make left The subscript position pointing to the first element of the array , Make right At the position of the subscript of the last element of the array .
int n = nums.size();
int left = 0;
int right = n-1;
int mid = 0; // The subscript of the intermediate element , It's defined in while Outside the loop is to avoid frequent creation , Because in while Inside the loop , The beginning of each cycle should be determined mid Size .
//<= Because left = right It makes sense .
while(left <= right){
// amount to mid = (left + right) / 2, Try not to write like this , Because when left and right When the value is large , Adding may cause data overflow .
mid = (right - left) /2 + left;
// If at present mid The element value at is exactly equal to target Words , Go straight back to mid
if(nums[mid] == target){
return mid;
}
// If at present mid The element value at is greater than target Words , Then it means that the target value to be found is mid The left side of the , So will right-1
else if(nums[mid] > target){
right = mid - 1;
continue;
}
// If at present mid Element value at is less than target Words , Then it means that the target value to be found is mid The right side of the , So will left-1
else{
left = mid + 1;
continue;
}
}
// This situation indicates that the target value is not nums Array , So back -1
return -1;
}
// Time complexity :O(logn), among n Is the length of the array .
// Spatial complexity :O(1).
};
边栏推荐
- Exploration of unity webgl
- Dual domain SSL certificate
- Moore Manor diary I: realize the reclamation, sowing, watering and harvest in Moore Manor
- Autowired注解警告的解决办法
- Unity3d lookat parameter description
- [control] multi agent system summary. 1. system model. 2. control objectives. 3. model transformation.
- Unity script life cycle and execution sequence
- 【Paper】2017_ Research on coordinated control method of underwater vehicle formation marine survey
- What is multimodal interaction?
- Unity a* road finding force planning
猜你喜欢
【Paper】2015_ Active fault-tolerant control system design with trajectory re-planning against actuator
Unity script life cycle and execution sequence
Singapore parent-child tour, these popular attractions must be arranged
This connection is not a private connection this website may be pretending to steal your personal or financial information
Connect to the database and run node JS running database shows that the database is missing
Pourquoi l'ordinateur n'a - t - il pas de réseau après l'ouverture du Hotspot win10?
Marvel fan welfare: Singapore Madame Tussauds Wax Museum Marvel 4D experience Museum
Bean creation process and lazy init delay loading mechanism
Approaching history, introduction to the London Guard Museum
Recommended cultural landmarks of these tourist attractions in Bangkok
随机推荐
MySQL查询小工具(一)json格式的字符串字段中,替换json数组中对象的某个属性值
Redis implements SMS login function (I) traditional session login
Solution to Autowired annotation warning
Unity is associated with vs. there is a compiler problem when opening
Dual domain SSL certificate
What is multimodal interaction?
【Paper】2019_ Consensus Control of Multiple AUVs Recovery System Under Switching Topologies and Time D
brew安装nvm报nvm command not found解决方案
Webots learning notes
Unity automatic pathfinding
LXC 和 LXD 容器总结
JS 数组的排序 sort方法详解
[UGV] schematic diagram of UGV version 32
Oculus quest2 development: (I) basic environment construction and guide package
Efficiency test of adding and querying ArrayList and LinkedList
Connect to the database and run node JS running database shows that the database is missing
Deep learning ----- different methods to realize inception-10
Unity a* road finding force planning
Yolov5 torch installation
力扣977. 有序数组的平方