当前位置:网站首页>【LeetCode Daily Question】——704. Binary Search
【LeetCode Daily Question】——704. Binary Search
2022-08-02 02:00:00 【IronmanJay】
一【题目类别】
- 二分查找
二【题目难度】
- 简单
三【题目编号】
- 704.二分查找
四【题目描述】
- 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1.
五【题目示例】
示例 1:
输入: nums = [-1,0,3,5,9,12], target = 9
输出: 4
解释: 9 出现在 nums 中并且下标为 4示例 2:
输入: nums = [-1,0,3,5,9,12], target = 2
输出: -1
解释: 2 不存在 nums 中因此返回 -1
六【题目提示】
- 你可以假设 nums 中的所有元素是不重复的.
- n 将在 [1, 10000]之间.
- nums 的每个元素都将在 [-9999, 9999]之间.
七【解题思路】
- Let me see who can't write binary search yet?
八【时间频度】
- 时间复杂度: O ( l o g 2 N ) O(log_{2}N) O(log2N),其中 N N N为数组元素个数
- 空间复杂度: O ( 1 ) O(1) O(1)
九【代码实现】
- Java语言版
package BinarySearch;
public class p704_BinarySearch {
public static void main(String[] args) {
int[] nums = {
-1, 0, 3, 5, 9, 12};
int res = search(nums, 9);
System.out.println("res = " + res);
}
public static int search(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (nums[mid] == target) {
return mid;
} else if (nums[mid] > target) {
right = mid - 1;
} else if (nums[mid] < target) {
left = mid + 1;
}
}
return -1;
}
}
- C语言版
#include<stdio.h>
int p704_BinarySearch_search(int* nums, int numsSize, int target)
{
int left = 0;
int right = numsSize - 1;
while (left <= right)
{
int mid = (left + right) / 2;
if (nums[mid] == target)
{
return mid;
}
else if (nums[mid] > target)
{
right = mid - 1;
}
else if (nums[mid] < target)
{
left = mid + 1;
}
}
return -1;
}
/*主函数省略*/
十【提交结果】
Java语言版
C语言版
边栏推荐
- Basic use of typescript34-class
- 求大神解答,这种 sql 应该怎么写?
- The ultra-large-scale industrial practical semantic segmentation dataset PSSL and pre-training model are open source!
- LeetCode刷题日记: 33、搜索旋转排序数组
- 用位运算为你的程序加速
- 3. Bean scope and life cycle
- typescript37-class的构造函数实例方法继承(extends)
- 秒懂大模型 | 3步搞定AI写摘要
- The Paddle Open Source Community Quarterly Report is here, everything you want to know is here
- 『网易实习』周记(一)
猜你喜欢
三本毕业的我被腾讯拒绝了十四次,最终成功入职阿里
"NetEase Internship" Weekly Diary (1)
大话西游创建角色失败解决
【轮式里程计】
Constructor instance method of typescript36-class
用位运算为你的程序加速
云和恩墨:让商业数据库时代的价值在openGauss生态上持续繁荣
Understand the big model in seconds | 3 steps to get AI to write a summary
Record the pits where an error occurs when an array is converted to a collection, and try to use an array of packaging types for conversion
Byte taught me a hard lesson: When a crisis comes, you don't even have time to prepare...
随机推荐
成都openGauss用户组招募啦!
『网易实习』周记(三)
待读书单列表
Check if IP or port is blocked
数据链路层的数据传输
Chengdu openGauss user group recruit!
外包干了三年,废了...
C语言之插入字符简单练习
【服务器数据恢复】服务器Raid5阵列mdisk磁盘离线的数据恢复案例
kubernetes之服务发现
手写一个博客平台~第三天
Hiring a WordPress Developer: 4 Practical Ways
MySQL optimization strategy
typescript30 - any type
一本适合职场新人的好书
typescript38-class的构造函数实例方法继承(implement)
3 Month Tester Readme: 4 Important Skills That Impacted My Career
喜报 | AR 开启纺织产业新模式,ALVA Systems 再获殊荣!
5年自动化测试经验的一些感悟:做UI自动化一定要跨过这10个坑
Constructor instance method of typescript36-class