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

边栏推荐
- typescript33-typescript高级概述
- Rust P2P Network Application Combat-1 P2P Network Core Concepts and Ping Program
- hash table
- AntPathMatcher uses
- Coding Experience Talk
- LeetCode Brushing Diary: 74. Searching 2D Matrix
- HSDC和独立生成树相关
- Huawei's 5-year female test engineer resigns: what a painful realization...
- "Introduction to Natural Language Processing Practice" Question Answering Robot Based on Knowledge Graph
- JDBC PreparedStatement 的命名参数实现
猜你喜欢
软件测试功能测试全套常见面试题【开放性思维题】面试总结4-3

typescript33-typescript高级概述

Garbage Collector CMS and G1

"NetEase Internship" Weekly Diary (2)

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

HSDC和独立生成树相关

Hash collisions and consistent hashing

Golang分布式应用之定时任务

Use baidu EasyDL implement factory workers smoking behavior recognition

Check if IP or port is blocked
随机推荐
When paying attention to the "Internet +" model, you usually only focus on the "Internet +" model itself
Handwriting a blogging platform ~ the first day
Day115. Shangyitong: Background user management: user lock and unlock, details, authentication list approval
C language inserted into the characters of simple exercises
飞桨助力航天宏图PIE-Engine地球科学引擎构建
60种特征工程操作:使用自定义聚合函数【收藏】
创新项目实战之智能跟随机器人原理与代码实现
A full set of common interview questions for software testing functional testing [open thinking questions] interview summary 4-3
"NetEase Internship" Weekly Diary (2)
手写一个博客平台~第一天
6-25漏洞利用-irc后门利用
垃圾回收器CMS和G1
Newton's theorem and related corollaries
Redis 底层的数据结构
【轮式里程计】
Chengdu openGauss user group recruit!
有效进行自动化测试,这几个软件测试工具一定要收藏好!!!
哈希冲突和一致性哈希
Byte taught me a hard lesson: When a crisis comes, you don't even have time to prepare...
LeetCode刷题日记:53、最大子数组和