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

边栏推荐
- HSDC is related to Independent Spanning Tree
- Fly propeller power space future PIE - Engine Engine build earth science
- 哈希冲突和一致性哈希
- Hiring a WordPress Developer: 4 Practical Ways
- 云和恩墨:让商业数据库时代的价值在openGauss生态上持续繁荣
- LeetCode brushing diary: 53, the largest sub-array and
- 3 Month Tester Readme: 4 Important Skills That Impacted My Career
- Rust P2P网络应用实战-1 P2P网络核心概念及Ping程序
- 用位运算为你的程序加速
- After graduating from three books, I was rejected by Tencent 14 times, and finally successfully joined Alibaba
猜你喜欢

Understand the big model in seconds | 3 steps to get AI to write a summary

外包干了三年,废了...

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

信息化和数字化的本质区别是什么?

typescript36-class的构造函数实例方法

【刷题篇】打家劫舍

kubernetes之服务发现

oracle查询扫描全表和走索引

"NetEase Internship" Weekly Diary (3)

Image fusion based on weighted 】 and pyramid image fusion with matlab code
随机推荐
搜罗汇总的效应
LeetCode Brushing Diary: 74. Searching 2D Matrix
当关注「互联网+」模式的时候,通常仅仅只是在关注「互联网+」模式本身
Image fusion based on weighted 】 and pyramid image fusion with matlab code
volatile原理解析
JDBC PreparedStatement 的命名参数实现
Record the pits where an error occurs when an array is converted to a collection, and try to use an array of packaging types for conversion
HSDC is related to Independent Spanning Tree
Coding Experience Talk
PHP 使用 PHPRedis 与 Predis
创新项目实战之智能跟随机器人原理与代码实现
For effective automated testing, these software testing tools must be collected!!!
typescript33-typescript高级概述
哈希冲突和一致性哈希
typescript38-class的构造函数实例方法继承(implement)
typescript35-class的构造函数
Effects of Scraping and Aggregation
Yunhe Enmo: Let the value of the commercial database era continue to prosper in the openGauss ecosystem
pcie inbound和outbound关系
【轮式里程计】