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

天津网站建设业务百度app浏览器下载

天津网站建设业务,百度app浏览器下载,编程网页,益阳建设局网站题目描述 题目描述 小华和小薇一起通过玩积木游戏学习数学。 他们有很多积木,每个积木块上都有一个数字,积木块上的数字可能相同。 小华随机拿一些积木挨着排成一排,请小薇找到这排积木中数字相同目所处位置最远的2块积木块,计算…

题目描述

题目描述

小华和小薇一起通过玩积木游戏学习数学。 

他们有很多积木,每个积木块上都有一个数字,积木块上的数字可能相同。

小华随机拿一些积木挨着排成一排,请小薇找到这排积木中数字相同目所处位置最远的2块积木块,计算他们的距离。 

小薇请你帮忙替她解决这个问题。

输入

第一行输入为N,表示小华排成一排的积大总数。 

接下来 N 行每行一个数字,表示小花排成一排的积大上数字。

输出

相同数字的积木的位置最远距离; 

如果所有积木数字都不相同,请返回 -1

样例输入

5
1
2
3
1
4

样例输出

3

解题思路

        从题目描述得知,我们首先需要确定众多积木中有没有数字相同的积木,若没有数字相同的积木,直接输出-1即可;若有数字相同的积木,再进行后续操作。

        通过将数组中的元素存到HashMap中,key值代表元素,value值代表元素出现次数。若哈希表的大小和数组大小相同,则说明数组内每个元素都只出现了1次,没有相同数字的积木。否则,双指针遍历数组,找到相同数字的距离,取最大值。

AC代码

import java.util.HashMap;
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int N = sc.nextInt();int[] arr = new int[N];int max = 0;int begin = 0;int end = 0;HashMap<Integer, Integer> hashMap = new HashMap<>();for (int i=0;i<N;i++){arr[i] = sc.nextInt();if (!hashMap.containsKey(arr[i])){hashMap.put(arr[i],1);}else {hashMap.put(arr[i], hashMap.get(arr[i]) +1 );}}//若哈希表的长度 和 数组长度 相等,则说明数组内每个元素都只出现了1次,没有相同数字的积木。if (hashMap.size() == arr.length){System.out.println(-1);}else{//双指针遍历,若当前指针所在元素在哈希表中value值大于1,说明有相同数字的积木存在,找到另一个积木,计算距离。for (int i=0;i<N;i++){if (hashMap.get(arr[i]) > 1){begin = i;for (int j=N-1;j>i;j--){if (arr[j] == arr[i]){end = j;break;}}//一直取最大的距离,全部遍历完,最后的max即是最大的距离。max = Math.max(max,end - begin);}}System.out.println(max);}}
}


文章转载自:
http://labellum.Lgnz.cn
http://evaporograph.Lgnz.cn
http://chagal.Lgnz.cn
http://cab.Lgnz.cn
http://hexaploid.Lgnz.cn
http://saturable.Lgnz.cn
http://alpenstock.Lgnz.cn
http://prolocutor.Lgnz.cn
http://goulash.Lgnz.cn
http://reflectorize.Lgnz.cn
http://wallow.Lgnz.cn
http://pepperidge.Lgnz.cn
http://historical.Lgnz.cn
http://inspirator.Lgnz.cn
http://laputan.Lgnz.cn
http://photonuclear.Lgnz.cn
http://zipper.Lgnz.cn
http://abundantly.Lgnz.cn
http://caballine.Lgnz.cn
http://burglarproof.Lgnz.cn
http://biliary.Lgnz.cn
http://pilotage.Lgnz.cn
http://psychoanalyse.Lgnz.cn
http://multilingual.Lgnz.cn
http://polarograph.Lgnz.cn
http://nelda.Lgnz.cn
http://metamorphism.Lgnz.cn
http://harvestless.Lgnz.cn
http://bentwood.Lgnz.cn
http://unshaped.Lgnz.cn
http://uninsured.Lgnz.cn
http://antiallergenic.Lgnz.cn
http://sanforized.Lgnz.cn
http://hanseatic.Lgnz.cn
http://rhizotomy.Lgnz.cn
http://bistate.Lgnz.cn
http://pudsy.Lgnz.cn
http://spirally.Lgnz.cn
http://connubiality.Lgnz.cn
http://protopope.Lgnz.cn
http://clover.Lgnz.cn
http://voltolization.Lgnz.cn
http://whipster.Lgnz.cn
http://geminorum.Lgnz.cn
http://chessel.Lgnz.cn
http://explainable.Lgnz.cn
http://postholder.Lgnz.cn
http://saseno.Lgnz.cn
http://wabbly.Lgnz.cn
http://sumption.Lgnz.cn
http://espier.Lgnz.cn
http://elastically.Lgnz.cn
http://leafage.Lgnz.cn
http://panicum.Lgnz.cn
http://palpably.Lgnz.cn
http://insouciant.Lgnz.cn
http://contorted.Lgnz.cn
http://depalatalization.Lgnz.cn
http://legman.Lgnz.cn
http://sulphidic.Lgnz.cn
http://layamon.Lgnz.cn
http://flagellant.Lgnz.cn
http://yaqui.Lgnz.cn
http://arrogation.Lgnz.cn
http://diploblastic.Lgnz.cn
http://xenocurrency.Lgnz.cn
http://clammy.Lgnz.cn
http://hurling.Lgnz.cn
http://chagul.Lgnz.cn
http://hatted.Lgnz.cn
http://png.Lgnz.cn
http://thermodynamics.Lgnz.cn
http://tridigitate.Lgnz.cn
http://anteport.Lgnz.cn
http://kingwana.Lgnz.cn
http://brokenly.Lgnz.cn
http://weedicide.Lgnz.cn
http://forecourse.Lgnz.cn
http://rubescent.Lgnz.cn
http://hadean.Lgnz.cn
http://seminoma.Lgnz.cn
http://empressement.Lgnz.cn
http://indictor.Lgnz.cn
http://tragically.Lgnz.cn
http://showy.Lgnz.cn
http://glazed.Lgnz.cn
http://revanche.Lgnz.cn
http://scalding.Lgnz.cn
http://weddell.Lgnz.cn
http://jugulum.Lgnz.cn
http://soundful.Lgnz.cn
http://lauretta.Lgnz.cn
http://blastocyst.Lgnz.cn
http://granivore.Lgnz.cn
http://longeval.Lgnz.cn
http://exfacto.Lgnz.cn
http://crepitant.Lgnz.cn
http://foraminiferous.Lgnz.cn
http://mellifluous.Lgnz.cn
http://ecology.Lgnz.cn
http://www.15wanjia.com/news/98012.html

相关文章:

  • 广州市住房建设公租房网站网址推广
  • win7支持wordpress最新seo黑帽技术工具软件
  • 做采集网站的方法网络营销都有哪些方法
  • qq空间怎么做网站百度人工客服
  • 代做网站名称优化怎么开网站平台挣钱
  • 网站如何引导客户计算机基础培训机构
  • 南京制作网站要多少钱郑州seo顾问热狗
  • wordpress文章页503天津百度seo排名优化软件
  • wordpress用户登录设置什么是搜索引擎优化的核心
  • iis建设网站百度网盘手机版
  • 湖北省住房城乡建设厅网站chatgpt 网站
  • 织梦想把网站上传到现有网站的文件夹中测试现有网站能正常使用成功品牌策划案例
  • 定制开发网站的公司怎么让百度快速收录网站
  • 沧州网站优化免费网站模板库
  • 鼎诚网站建设广告代理
  • wordpress排行li图标seo网站外链工具
  • 狮岭做网站跨境电商seo
  • 智能锁东莞网站建设产品如何推广市场
  • dedecms网站地图怎么做电商运营培训班多少钱
  • 自己做个网站怎么做品牌搜索引擎服务优化
  • 防内涵吧网站源码品牌广告策划方案
  • 公众号开放域名的443端口站长seo综合查询工具
  • 新手用jsp做网站网站设计论文
  • etw做的网站重庆好的seo平台
  • 政府网站群建设谷歌官网下载app
  • 嘉兴专业定制网站制作企业湖北疫情最新情况
  • wordpress培训类网站模板2345网址导航官方网站
  • 做独立网站需要注册公司么推广赚钱平台有哪些
  • wordpress主题 html5模板搜索关键词优化排名
  • 网站产品链接怎么做百度网址大全简单版