LeetCode解题报告--Search for a Range

Uncertainty-Aware Mean Teacher(UA-MT) 本文提出了一种新的基于不确定性的半监督学习框架(UA-MT),通过额外利用未标记的数据从3D MR图像中分割左心房。具体地说,本文建立了一个教师模型和一个学生模型,学生模型通过最小化标注数据上的分割监督损失和所有输入数据上的与教师模型预测输出的一致性损失进行优化。如果没有未标记输入中的注释,教师模型的预测目标可能不可靠且有噪声。:在估计不确定性 U 的指导下,我们过滤掉相对不可靠**(高不确定性),整个体积** U 的不确定性**是 {u} ∈ RH×W ×D。过滤掉高不确定性的,也就是熵大于某个值的。 阅读详情

题目:

Given a sorted array of integers, find the starting and ending position of a given target value.
Your algorithm’s runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1].
For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].
点击解题:Search for a Range

分析:题意要求找目标数组在出已排序数列的范围。
题目难度不大,注意时间复杂度O(logn),是该题的关键,三种解题思路:
1)暴力法直接遍历(超时):直接用for循环从左到右遍历,时间复杂度为O(n),超时。先找记录第一次nums[i] = target,array[0] = i,之后继续遍历,直至nums[i] != target,array[1] = i。
2)二分查找法(可行):先找到nums[i] == target,不管i是左边界还是右边界,或是中间值,即找到第一个nums[i] = target, 之后在找到的i基础上,向右遍历找到右边界,向左遍历,找到左边界。注意当找到第一个i =0 (或 i = nums.length - 1),则左(或右)边界找到,只需找右(或左)边界,加个判断。
3)双指针发(可行):双指针left和right分别指向数组的起始,即left = 0,right = nums.length - 1,之后,先从左向右遍历,找到第一个nums[i] = target,left = i, 此时left为左边界;接着再从右向左遍历,找到第一个nums[i] = target,right = i,此时right为右边界。

如下示意图示:
这里写图片描述

Java代码:Accepted
Binary Search:

public class Solution {
    public int[] searchRange(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        //term, termq for termporary variable, save the first middle
        int tem = 0;
        int tem1 = 0;
        //sign whether find target
        int flag = 0;
        int[] array = new int[] { -1, -1 };

        //Binary search to find first middle nums[middle] = target
        while (left <= right) {
            int middle = (left + right) / 2;
            if (nums[middle] == target) {
                tem = middle;
                tem1 = middle;
                flag = 1;
                break;
            } else if (nums[middle] < target) {
                left = middle + 1;
            } else {
                right = middle - 1;
            }
        }

        //if not return[-1,-1]
        if (flag == 0) {
            return array;
        }

        //find the right boundary
        if(tem != nums.length - 1){
            while (tem < nums.length) {
                if ((tem + 1 < nums.length) && nums[tem] == nums[tem + 1]) {
                    tem++;
                } else {
                    array[1] = tem;
                    break;
                }
            }
        }else{
            array[1] = tem;
        }

        //find left boundary
        if(tem1 != 0){
            while (tem1 > -1) {
                if ((tem1 - 1 > -1) && nums[tem1] == nums[tem1 - 1]) {
                    tem1--;
                } else {
                    array[0] = tem1;
                    break;
                }
            }
        }else{
            array[0] = tem1;
        }

        return array;
    }
}

Tow Points Search:

public class Solution {
    public int[] searchRange(int[] nums, int target) {

        int[] array = new int[]{-1,-1};

        //tow points for search nums[left] == target and nums[right] == target
        int left = 0;
        int right = nums.length - 1;
        //flagL for left index is found and flagR for right index is found
        int flagL = 0;
        int flagR = 0;

        //Find left index
        while(left <= right){

            if(flagL == 0 && nums[left] == target){
                array[0] = left;
                flagL = 1;
                break;
            }else{
                left ++;
            }

        }

        //Find right index
        while(right >= left){

            if(flagR == 0 && nums[right] == target){
                array[1] = right;
                flagR = 1;
                break;
            }else{
                right --;
            }
        }

        //No find target
        if(flagL == 0 && flagR == 0){
            return array;
        }

        return array;
    }
}

Python代码 Accepted:
Two Points Search:

class Solution(object):
    def searchRange(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        #array for the result
        array = [-1,-1]
        #flagL for left index is found
        flagL = 0
        #flagR for right index is found
        flagR = 0

        #two points left and righr
        left = 0
        right = len(nums) - 1

        #found left index
        while(left <= right):
            if(nums[left] == target):
                array[0] = left
                flagL = 1
                break
            else:
                left += 1
        #found right index    
        while(right >= left):
            if(nums[right] == target):
                array[1] = right
                flagR = 1
                break
            else:
                right -= 1
        #no found target
        if(flagL == 0 and flagR == 0):
            return array

        return array
Springboot——接入企业微信聊天机器人 最近企业微信的聊天机器人很火也很好玩,本期想自己接入一个玩玩试试水,同时也接入AI大模型进行数据的交互。 阅读详情

相关推荐

别再折腾环境了!Windows 11 + VSCode + CMake 3.31.4 + MinGW-w64 一键式配置指南(附常见报错解决)

本文提供Windows 11下VSCode、CMake 3.31.4和MinGW-w64的一键式配置指南,帮助开发者快速搭建高效C++开发环境。涵盖工具链选型、环境变量配置、项目初始化及常见报错解决方案,大幅提升开发效率。

weixin_28841265的博客 186

LeetCode 34. Search for a Range(有序数组,查找给定值范围)

题目描述:    Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.    Your algorithm's runtime complexity must be in the order of O(log n).  ...

andersonxie的博客 463

centos7安装Ambari 2.7.5 + HDP3.1.5

准备 1.安装包准备: Ambari2.7.5、HDP31.5、libtirpc-devel: centos7操作系统: 链接:https://pan.baidu.com/s/1vzvio5rAMsbYF0-0vBs5dw 提取码:xij9

kxq的博客 5091

Search for a Range 两种解法

自己的版本,二分查找,找到的时候liy

jitashiwoxihuande的专栏 614

Mac用户解决WPS中的俄语显示红线问题(WPS添加外语拼写检查)

我所使用的WPS版本:AppleStore 下载的 MacOs版 3.3.0 首先,对于 在访达-应用程序中,找到WPS,右键-点击“显示包内容” 然后按照下图的索引目录(Contents\Resource\office6\dicts\spellcheck),最后进入一个叫“spellcheck”的文件夹 最后将下载好的字典包(例如上图中的 en_US就代表美式英语;ru_RU就代表俄语) 关于字典包资源下载,我在网上找到一个Google云盘,里面包含了法语,德语,西语,意大利语,葡萄牙.

qq_48159150的博客 7893

药物不良反应预测实战指南:从FAERS数据挖掘到PyTorch深度学习模型(含可解释性分析)

【代码】药物不良反应预测实战指南:从FAERS数据挖掘到PyTorch深度学习模型(含可解释性分析)

理论都是虚的,代码才是王道。 721

Leetcode刷题记——34. Search for a Range(查找一个范围)

一、题目叙述: Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in th

爱胡椒的小白兔呢 443

数据分析案例-电影数据可视化分析

数据介绍数据为2011-2021电影数据可视化分析首先导入本次项目需要的包和数据 数据预处理 可视化

m0_64336780的博客 2万+

leetcode:Search for a Range

一、 题目 给出一个已经排好序的数组和一个数字,找出在这个数字在数组中的范围。 例如:[5,7,7,8,8,10]和8 返回:[3,4] 二、 分析 题目很基础,就是找出等值的数字的开始和结束索引,上去就遍历当第一次遇到等值时记录下左索引,最后一次遇到记录下右索引也不会超时。如下: //遍历法 class Solution { public: vector searchRang

恰同学少年 932

Spring 核心技术解析【纯干货版】- I:Spring 基础模块 Spring-Core 模块精讲

Spring-Core 模块,是 Spring 核心容器模块之一。虽然 “Core” 本身就有 “核心的,重要的部分” 的含义,但是它并非是实现(常被我们说为 Spring 核心机制)IOC / AOP 的模块。Spring 中的 IOC 机制,主要来自于 BeanFactory 容器和 ApplicationContext 容器,这二者分别由同为核心容器模块的 Spring-Bean 模块 和 Spring-Context 模块所提供。

栗筝i的博客 2522

模拟电路综合笔试题解析

本专栏预计更新60期,当前为第10期,文章约6000字,旨在帮助读者掌握模拟电路笔试的核心知识点与实战技巧。文章分为五个章节,涵盖基础理论、高频考点、大厂题目解析等内容。首先,文章详细解析了DC-DC与LDO的区别及选型依据,并通过真题分析帮助读者理解其应用场景。接着,文章深入探讨了运放电路设计、同步降压电路设计等高频考点,并结合实际案例提供解决方案。此外,文章还分享了特斯拉、华为、大疆等大厂的面试题及解析,帮助读者应对复杂电路设计挑战。最后,文章总结了模拟电路设计中的关键技巧与优化方法,为读者提供全面的笔

启芯硬件的博客 468

LeetCode Search for a Range搜索特定数值的范围 三种方法求解

Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order ofO(logn). If the target is not found in the...

架构设计 220

基于STM32、HAL库的CH9434总线转UART驱动程序设计

CH9434是一款USB转4串口控制芯片,可以将单个USB接口扩展为4个独立的串口(UART)。支持多种数据格式(数据位5-8,停止位1/1.5/2,奇偶校验或无校验)// 读取接收到的数据。// 重新启动接收中断。// 向所有4个串口发送数据。// UART初始化函数示例(USART1)// 其他UART初始化函数类似...// 初始化CH9434。// 初始化UART1。// 初始化UART2。// 初始化UART3。// 初始化UART4。工作电压:3.3V/5V兼容。

corlin6688的博客 989

LeetCode: Search for a Range 解题报告

Search for a RangeGiven a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the target i...

weixin_34362790的博客 107

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

Leetcode Search for a Range 解题报告

二分搜索的一个变种,找到target存在的区间。

算法的天空 1318

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

[leetcode] 34. Search for a Range 解题报告

题目链接:https://leetcode.com/problems/search-for-a-range/ Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must

小榕流光的专栏 1079

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

[LeetCode] Search for a Range 解题报告

Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm’s runtime complexity must be in the order of O(log n). If the target is not found in the

codingtmd_in_china的博客 223

LeetCode解题报告—— Search in Rotated Sorted Array & Search for a Range & Valid Sudoku

1.Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated(轮流,循环) at some pivot(枢轴; 中心点) unknown to you beforehand(提前; 事先). (i.e.,0 1 2 4 5 6 7might become4 5 6 ...

dbvz4901的博客 146

[Leetcode] 34. Search for a Range 解题报告

题目: Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the target is n

魔豆(Magicbean)的博客 473

Leetcode #34. Search for a Range 范围搜寻 解题报告

1 解题思想题目给了一个允许重复的,已排序的数组,和一个目标值,让你把这个值所出现的位置都找出来,难度在于,要求时间复杂度为O(log n).既然时间复杂度要求O(log n),那么肯定是要用一些技巧的,我的方法就是二分查找了,两次二分查找,分别找头和尾,思想都是一样的,只是处理的略有不同罢了2 原题Given a sorted array of integers, find the startin

MebiuW的专栏 944

[Leetcode] 715. Range Module 解题报告

题目: A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the following interfaces in an efficient manner. addRange(int left, int right) Adds the h

魔豆(Magicbean)的博客 1201

[leetcode javascript解题]Search for a range

leetcode 第34题”Search for a range”描述如下: Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm’s runtime complexity must be in the order of

zzxboy1的博客 661

LeetCode034 Search for a Range

详细见:leetcode.com/problems/search-for-a-range Java Solution: github package leetcode; public class P034_SearchForARange { public static void main(String[] args) { new Solution().searchRange

zxwtry的博客 382

[Leetcode] Search for a Range

Search for a Range 题解 题目来源:https://leetcode.com/problems/search-for-a-range/description/ Description Given an array of integers sorted in ascending order, find the starting and ending position of a g...

weixin_30632899的博客 88

Leetcode 34 Search for a Range

Given an array of integers sorted in ascending order, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order ofO(logn). If the...

diaojie6179的博客 152

Search for a Range ——LeetCode

Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order ofO(logn). If the target is not found...

84
上一篇: LeetCode解题报告--Next Permutation
下一篇: LeetCode解题报告--Search Insert Position
YatKam
博客等级 码龄12年 58粉丝 49原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值