#yyds干货盘点# 面试必刷TOP101:二分查找-I
source link: https://blog.51cto.com/u_15488507/5563491
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
#yyds干货盘点# 面试必刷TOP101:二分查找-I
原创1.简述:
描述请实现无重复数字的升序数组的二分查找
给定一个 元素升序的、无重复数字的整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标(下标从 0 开始),否则返回 -1
数据范围: , 数组中任意值满足
进阶:时间复杂度 ,空间复杂度
示例12.代码实现:
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型一维数组
* @param target int整型
* @return int整型
*/
public int search (int[] nums, int target) {
// write code here
int left = 0, right = nums.length - 1;
while(left <= right){
int mid = (left + right) >> 1;
if(nums[mid] == target){
return mid;
} else if(nums[mid] > target){
right = mid - 1;
} else {
left = mid + 1;
}
}
return -1;
}
}
- 赞
- 收藏
- 评论
- 分享
- 举报
Recommend
-
9
#yyds干货盘点# 面试必刷TOP101:反转链表 原创 97的风 2022-07-25 15:14...
-
6
#yyds干货盘点# 面试必刷TOP101:链表内指定区间反转 原创 97的风 2022-0...
-
3
#yyds干货盘点# 面试必刷TOP101: 删除链表的倒数第n个节点 原创 97的风 ...
-
3
#yyds干货盘点# 面试必刷TOP101:两个链表的第一个公共结点 原创 97的风 ...
-
6
#yyds干货盘点# 面试必刷TOP101:链表相加(二) 原创 97的风 2022-08-04 1...
-
9
#yyds干货盘点# 面试必刷TOP101:删除有序链表中重复的元素-II 原创 97的风
-
3
#yyds干货盘点# 面试必刷TOP101:二维数组中的查找 原创 97的风 2022-08-...
-
9
#yyds干货盘点# 面试必刷TOP101:比较版本号 原创 97的风 2022-08-15 13:...
-
7
#yyds干货盘点# 面试必刷TOP101:二叉树的前序遍历 精选 原创 97的风 20...
-
6
#yyds干货盘点# 面试必刷TOP101:二叉树的中序遍历 精选 原创 97的风 20...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK