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

边栏推荐
- bool Frame::PosInGrid(const cv::KeyPoint &kp, int &posX, int &posY)
- A full set of common interview questions for software testing functional testing [open thinking questions] interview summary 4-3
- Day115.尚医通:后台用户管理:用户锁定解锁、详情、认证列表审批
- 力扣 1161. 最大层内元素和
- Typescript31 - any type
- LeetCode刷题日记: 33、搜索旋转排序数组
- Fly propeller power space future PIE - Engine Engine build earth science
- 安全(1)
- LeetCode刷题日记:34、 在排序数组中查找元素的第一个和最后一个位置
- Constructor instance method inheritance of typescript38-class (implement)
猜你喜欢

typescript30 - any type

力扣 1374. 生成每种字符都是奇数个的字符串

Navicat数据显示不完全的解决方法

AOF rewrite

哈希冲突和一致性哈希

Fundamentals of Cryptography: X.690 and Corresponding BER CER DER Encodings

Day115.尚医通:后台用户管理:用户锁定解锁、详情、认证列表审批

Entry name ‘org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt’ collided

Day116. Shangyitong: Details of appointment registration ※

Use baidu EasyDL implement factory workers smoking behavior recognition
随机推荐
6-24漏洞利用-vnc密码破解
乱七八糟的网站
电商库存系统的防超卖和高并发扣减方案
密码学的基础:X.690和对应的BER CER DER编码
Anti-oversold and high concurrent deduction scheme for e-commerce inventory system
【轮式里程计】
三本毕业的我被腾讯拒绝了十四次,最终成功入职阿里
Shell入门终章
For effective automated testing, these software testing tools must be collected!!!
CodeTon Round 2 D. Magical Array 规律
一本适合职场新人的好书
C语言之插入字符简单练习
华为5年女测试工程师离职:多么痛的领悟...
volatile原理解析
大话西游无法登陆解决
数据链路层的数据传输
LeetCode刷题日记: 33、搜索旋转排序数组
【刷题篇】打家劫舍
When paying attention to the "Internet +" model, you usually only focus on the "Internet +" model itself
3. Bean scope and life cycle