当前位置:网站首页>【LeetCode每日一题】——704.二分查找
【LeetCode每日一题】——704.二分查找
2022-08-02 01:57: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]之间。
七【解题思路】
- 让我看看是谁还不会写二分查找?
八【时间频度】
- 时间复杂度: 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语言版

边栏推荐
- 编码经验之谈
- 【ORB_SLAM2】SetPose、UpdatePoseMatrices
- LeetCode Review Diary: 34. Find the first and last position of an element in a sorted array
- "NetEase Internship" Weekly Diary (3)
- 浅谈国产ERP的“横纵竖”三向发展态势
- 2023年起,这些地区软考成绩低于45分也能拿证
- 『网易实习』周记(一)
- Navicat data shows incomplete resolution
- 华为5年女测试工程师离职:多么痛的领悟...
- 超大规模的产业实用语义分割数据集PSSL与预训练模型开源啦!
猜你喜欢

Day116. Shangyitong: Details of appointment registration ※

6-25 Vulnerability Exploitation - irc Backdoor Exploitation

The Paddle Open Source Community Quarterly Report is here, everything you want to know is here

LeetCode刷题日记:74. 搜索二维矩阵

The ultra-large-scale industrial practical semantic segmentation dataset PSSL and pre-training model are open source!

数据链路层的数据传输

typescript31-any类型

typeof in typescript32-ts

大话西游创建角色失败解决

LeetCode刷题日记:LCP 03.机器人大冒险
随机推荐
Anti-oversold and high concurrent deduction scheme for e-commerce inventory system
The Paddle Open Source Community Quarterly Report is here, everything you want to know is here
使用百度EasyDL实现厂区工人抽烟行为识别
求大神解答,这种 sql 应该怎么写?
typescript35-class的构造函数
记录一次数组转集合出现错误的坑点,尽量使用包装类型数组进行转换
AntPathMatcher uses
AntPathMatcher使用
typescript32-ts中的typeof
Constructor instance method inheritance of typescript38-class (implement)
有效进行自动化测试,这几个软件测试工具一定要收藏好!!!
Fly propeller power space future PIE - Engine Engine build earth science
Shell入门终章
Local storage in Kubernetes
Constructor instance method of typescript36-class
"NetEase Internship" Weekly Diary (2)
力扣、752-打开转盘锁
LeetCode Review Diary: 34. Find the first and last position of an element in a sorted array
Byte taught me a hard lesson: When a crisis comes, you don't even have time to prepare...
LeetCode刷题日记: 33、搜索旋转排序数组