当前位置: 首页 > news >正文

【练习】二分查找

1、704

(1)题目描述

(2)代码实现

package com.hh.practice.leetcode.array.demo_02;public class BinarySearch_704 {public int search(int[] nums, int target) {int i= 0,j = nums.length -1;while (i <= j){int mid = (i+j) >>> 1;if (target < nums[mid]){j = mid - 1;} else if (nums[mid] < target) {i = mid + 1;}else {return mid;}}return -1;}//方式一
//    public int search(int[] nums, int target) {
//        for(int i = 0; i < nums.length; i++){
//            if(target == nums[i]){
//               return i;
//            }
//        }
//        return -1;
//    }
}

2、35

(1)题目描述

(2)代码实现

package com.hh.practice.leetcode.array.demo_02;public class BinarySearch_35 {//方式二public static int searchInsert(int[] arr, int key) {int i = 0;int j = arr.length - 1;while (i <= j) {int mid = (i + j) >>> 1;if (key <= arr[mid]) {j = mid - 1;} else{i = mid + 1;}}return i;}//方式一
//    public int searchInsert(int[] nums, int target) {
//        int low = 0;
//        int high = nums.length - 1;
//
//        while (low <= high) {
//            int mid = (low + high) >>> 1;
//            int midVal = nums[mid];
//
//            if (midVal < target)
//                low = mid + 1;
//            else if (midVal > target)
//                high = mid - 1;
//            else
//                return mid; // target found
//        }
//        //没找到就返回插入点,
//        return low;  // target not found.
//    }
}

3、34

(1)题目描述‘

(2)代码实现

package com.hh.practice.leetcode.array.demo_02;public class BinarySearch_34 {public int[] searchRange(int[] nums, int target) {int[] arr = new int[2];arr[0] = binaryLeft(nums,target);if (arr[0] == -1){arr[1] = -1;} else {arr[1] = binaryRight(nums, target);}return arr;}/*返回最靠左的元素索引*/public int binaryLeft(int[] arr, int key) {int i = 0;int j = arr.length - 1;int candidate = -1;while (i <= j) {int mid = (i + j) >>> 1;if (key < arr[mid]) {j = mid - 1;} else if (key > arr[mid]) {i = mid + 1;} else {candidate = mid;j = mid -1;}}return candidate;}/*返回最靠右的元素索引*/public int binaryRight(int[] arr, int key) {int i = 0;int j = arr.length - 1;int candidate = -1;while (i <= j) {int mid = (i + j) >>> 1;if (key < arr[mid]) {j = mid - 1;} else if (key > arr[mid]) {i = mid + 1;} else {candidate = mid;i = mid +1;}}return candidate;}
}

 


 

本文为学习笔记,所参考文章均已附上链接,若有疑问请私信!

创作不易,如果对你有点帮助的话麻烦点个赞支持一下!

新手小白,欢迎留言指正!

http://www.lryc.cn/news/340594.html

相关文章:

  • FactoryTalk View 上位机画面版本升级,还原和备份
  • 【微信小程序】分包
  • Golang教程六(单元测试,反射,网络编程,部署)
  • mybatis进阶篇-执行CRUD操作-typeAliases别名-接口绑定
  • C#面:泛型的主要约束和次要约束是什么
  • Java使用documents4j将word和excel转pdf
  • 使用策略模式实现 Spring 分布式和单机限流
  • @CrossOrigin注解解决跨域问题
  • 【力扣】45. 跳跃游戏 II
  • 【Python基础】19.eval函数的使用
  • 对装饰器模式的理解
  • 在替换微软AD的CA证书服务AD CS前,要先做哪些准备工作?
  • Java中的System
  • Mybites一对多collection
  • 基于springboot实现图书进销存管理系统项目【项目源码+论文说明】计算机毕业设计
  • 敏捷开发:想要快速交付就必须舍弃产品质量?
  • SNMP-详解指南
  • vue-router 原理【详解】hash模式 vs H5 history 模式
  • WebGl/Three 粒子系统 人物破碎及还原运动
  • 华为OD-C卷-分披萨[100分]
  • uniapp 中video标签视频禁止快,拖拽快进
  • 网页端HTML使用MQTTJs订阅RabbitMQ数据
  • 课题学习(二十一)----姿态更新的四元数算法推导
  • NL2SQL进阶系列(5):论文解读业界前沿方案(DIN-SQL、C3-SQL、DAIL-SQL、SQL-PaLM)、新一代数据集BIRD-SQL解读
  • 双指针运用:删除重复元素、移除元素
  • 什么是三高架构
  • Unity 对APK签名
  • 合成孔径雷达干涉测量InSAR数据处理、地形三维重建、形变信息提取、监测等应用
  • QT进阶------------------QPushButton(快速添加按钮与使用)
  • Vue项目管理器创建项目