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

边栏推荐
- Navicat数据显示不完全的解决方法
- 喜报 | AR 开启纺织产业新模式,ALVA Systems 再获殊荣!
- 搜罗汇总的效应
- When paying attention to the "Internet +" model, you usually only focus on the "Internet +" model itself
- typescript29-枚举类型的特点和原理
- Constructor of typescript35-class
- LeetCode刷题日记:53、最大子数组和
- Rust P2P网络应用实战-1 P2P网络核心概念及Ping程序
- 软件测试 接口自动化测试 pytest框架封装 requests库 封装统一请求和多个基础路径处理 接口关联封装 测试用例写在yaml文件中 数据热加载(动态参数) 断言
- Data transfer at the data link layer
猜你喜欢

一本适合职场新人的好书

5年自动化测试经验的一些感悟:做UI自动化一定要跨过这10个坑

软件测试 接口自动化测试 pytest框架封装 requests库 封装统一请求和多个基础路径处理 接口关联封装 测试用例写在yaml文件中 数据热加载(动态参数) 断言

The characteristics and principle of typescript29 - enumeration type

三本毕业的我被腾讯拒绝了十四次,最终成功入职阿里

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

成都openGauss用户组招募啦!

【ORB_SLAM2】void Frame::ComputeImageBounds(const cv::Mat &imLeft)

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

Constructor of typescript35-class
随机推荐
Chengdu openGauss user group recruit!
LeetCode brushing diary: 33. Search and rotate sorted array
记录一次数组转集合出现错误的坑点,尽量使用包装类型数组进行转换
力扣、752-打开转盘锁
第一次写对牛客的编程面试题:输入一个字符串,返回该字符串出现最多的字母
typescript30-any类型
Pcie the inbound and outbound
Redis 底层的数据结构
Moonbeam与Project Galaxy集成,为社区带来全新的用户体验
Basic use of typescript34-class
哈希冲突和一致性哈希
哈希表
Use baidu EasyDL implement factory workers smoking behavior recognition
【刷题篇】打家劫舍
创新项目实战之智能跟随机器人原理与代码实现
Huawei's 5-year female test engineer resigns: what a painful realization...
The ultra-large-scale industrial practical semantic segmentation dataset PSSL and pre-training model are open source!
Yunhe Enmo: Let the value of the commercial database era continue to prosper in the openGauss ecosystem
Entry name ‘org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt’ collided
6-24漏洞利用-vnc密码破解