当前位置:网站首页>【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语言版

边栏推荐
- 6-25 Vulnerability Exploitation - irc Backdoor Exploitation
- Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021:解读
- 字节给我狠狠上了一课:危机来的时候你连准备时间都没有...
- Kubernetes之本地存储
- 力扣、752-打开转盘锁
- ofstream,ifstream,fstream读写文件
- hash table
- 哈希冲突和一致性哈希
- Chengdu openGauss user group recruit!
- Entry name ‘org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt’ collided
猜你喜欢
A full set of common interview questions for software testing functional testing [open thinking questions] interview summary 4-3

LeetCode刷题日记:LCP 03.机器人大冒险

手写一个博客平台~第三天

Fly propeller power space future PIE - Engine Engine build earth science

字节给我狠狠上了一课:危机来的时候你连准备时间都没有...

2023年起,这些地区软考成绩低于45分也能拿证

HSDC is related to Independent Spanning Tree

Local storage in Kubernetes

MySQL8 下载、启动、配置、验证

typescript30 - any type
随机推荐
Local storage in Kubernetes
Navicat数据显示不完全的解决方法
For effective automated testing, these software testing tools must be collected!!!
Fly propeller power space future PIE - Engine Engine build earth science
typescript37-class的构造函数实例方法继承(extends)
Constructor instance method of typescript36-class
用位运算为你的程序加速
3 Month Tester Readme: 4 Important Skills That Impacted My Career
The Paddle Open Source Community Quarterly Report is here, everything you want to know is here
云和恩墨:让商业数据库时代的价值在openGauss生态上持续繁荣
飞桨助力航天宏图PIE-Engine地球科学引擎构建
pcie inbound和outbound关系
HSDC is related to Independent Spanning Tree
LeetCode刷题日记:53、最大子数组和
Understand the big model in seconds | 3 steps to get AI to write a summary
超大规模的产业实用语义分割数据集PSSL与预训练模型开源啦!
力扣、752-打开转盘锁
求大神解答,这种 sql 应该怎么写?
飞桨开源社区季度报告来啦,你想知道的都在这里
Handwriting a blogging platform ~ the first day