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

清华大学学生工作做网站互联网营销师培训学校

清华大学学生工作做网站,互联网营销师培训学校,建筑工人找活的平台,移动应用开发大作业数组中出现次数超过一半的数字 问题描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。你可以假设数组是非空的,并且给定的数组总是存在多数元素。详见剑指offer39 问题分析 最直接的方式就是使用hashMap,遍历给定数组&#xff0c…

数组中出现次数超过一半的数字

问题描述

数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。你可以假设数组是非空的,并且给定的数组总是存在多数元素。详见剑指offer39

问题分析

最直接的方式就是使用hashMap,遍历给定数组,将数字和对应出现次数存储在hashMap中,然后再遍历hashMap,找到出现次数最大的数字。除此之外,我们还可以将数据进行排序,升序和降序均可,排序后,出现次数超过一半的元素一定出现在数组的中位数位置。除此之外,还有一种巧妙解法,设立两个变量,num和count,num用于存储当前遍历到的元素,count用于存储次数,如果count为0,则当前元素不可能是出现次数超过一半的元素,则遍历下一个元素。

代码实现

使用HashMap解法

public int majorityElement(int[] nums) {Map<Integer,Integer> map = new HashMap<>();for(int i=0;i<nums.length;i++){if(map.containsKey(nums[i])){map.put(nums[i],map.get(nums[i])+1);}else{map.put(nums[i],1);}}int maxCount = Integer.MIN_VALUE;int maxNum = Integer.MIN_VALUE;for(int num:map.keySet()){if(map.get(num)>maxCount){maxCount = map.get(num);maxNum = num;}}return maxNum;
}

使用排序解法

public int majorityElement(int[] nums) {Arrays.sort(nums);return nums[nums.length/2];
}

巧妙解法

public int majorityElement(int[] nums) {int result=0;int count=0;for(int num:nums){if(count==0){result = num;}count += result==num?1:-1;}return result;
}

数组中只出现一次的数字

问题描述

给你一个 非空 整数数组 nums ,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。详见leetcode136

问题分析

使用HashMap存储数组元素和元素出现的次数,遍历HashMap,找到出现一次的元素,或者利用HashSet存储元素,利用集合的不可重复性,如果可以添加到集合中,说明当前遍历到的元素在数组中出现一次,直接添加,如果不能添加,说明当前遍历到的元素在数组中出现两次,移除HashSet中的当前元素,最后返回HashSet中的元素,即为数组中只出现一次的元素。除此之外,我们还可以利用位运算来实现。遍历数组元素,进行异或运算,出现两次的元素异或运算结果为0,所有元素的异或运算结果为数组中只出现一次的元素

代码实现

使用HashSet

public int singleNumber(int[] nums) {Set<Integer> set = new HashSet<>();for(int num: nums){if(!set.add(num)){set.remove(num);}}Integer[] array = set.toArray(new Integer[set.size()]);return array[0];
}

使用异或运算

public int singleNumber(int[] nums) {int res = 0;for(int num:nums){res^=num;}return res;
}

137. 只出现一次的数字 II

问题描述

给你一个整数数组 nums ,除某个元素仅出现 一次 外,其余每个元素都恰出现 三次 。请你找出并返回那个只出现了一次的元素。

问题分析

使用HashMap存储数组元素和元素出现的次数,遍历HashMap,找到出现一次的元素。另外我们也可以使用位运算来实现对于出现3次的元素,每一位为0或者1,相加为0或者3,可以将每一位相加后对3取余,即为只出现一次的元素的对应位的值。

代码实现

使用位运算实现

public int singleNumber(int[] nums) {int res = 0;for(int i=0;i<32;i++){int total = 0;for(int num:nums){total+=((num>>i)&1);}if(total%3!=0){res |= (1<<i);}}return res;
}

总结

元素出现次数问题通用方法就是使用HashMap存储元素和对应次数,或许遍历map即可得到想要出现次数的元素。这种方法需要遍历一次数组,遍历一次map,使用一个map,时间复杂度为O(n),空间复杂度为O(n),面试时,可能不允许使用Hash或者集合,即需要我们设计O(n)时间复杂度,常数空间复杂度的算法。此时我们可以考虑常数计数或者为运算等常见方式。


文章转载自:
http://antiutopian.xhqr.cn
http://blot.xhqr.cn
http://skedaddle.xhqr.cn
http://dee.xhqr.cn
http://microcopy.xhqr.cn
http://yielding.xhqr.cn
http://hih.xhqr.cn
http://izzat.xhqr.cn
http://clavate.xhqr.cn
http://teaplanting.xhqr.cn
http://eyrir.xhqr.cn
http://coextend.xhqr.cn
http://establishment.xhqr.cn
http://barothermohygrogram.xhqr.cn
http://sensually.xhqr.cn
http://rifampicin.xhqr.cn
http://unate.xhqr.cn
http://adventureful.xhqr.cn
http://cystoscopic.xhqr.cn
http://aerolite.xhqr.cn
http://californiate.xhqr.cn
http://lumisterol.xhqr.cn
http://raised.xhqr.cn
http://theoretically.xhqr.cn
http://espantoon.xhqr.cn
http://hinoki.xhqr.cn
http://preconceive.xhqr.cn
http://cctv.xhqr.cn
http://gangland.xhqr.cn
http://bindery.xhqr.cn
http://deltawinged.xhqr.cn
http://dictograph.xhqr.cn
http://monorheme.xhqr.cn
http://astaticism.xhqr.cn
http://highbred.xhqr.cn
http://tsamba.xhqr.cn
http://celaeno.xhqr.cn
http://trimphone.xhqr.cn
http://sceneshifter.xhqr.cn
http://chirognomy.xhqr.cn
http://dermatosis.xhqr.cn
http://clubbed.xhqr.cn
http://canticle.xhqr.cn
http://portly.xhqr.cn
http://mandarine.xhqr.cn
http://crouch.xhqr.cn
http://houseguest.xhqr.cn
http://allusive.xhqr.cn
http://stan.xhqr.cn
http://tollgatherer.xhqr.cn
http://antiquarianism.xhqr.cn
http://humpbacked.xhqr.cn
http://thermoplastic.xhqr.cn
http://labourer.xhqr.cn
http://whore.xhqr.cn
http://vibist.xhqr.cn
http://avitaminosis.xhqr.cn
http://intelligibility.xhqr.cn
http://nejd.xhqr.cn
http://textured.xhqr.cn
http://uncrumple.xhqr.cn
http://cotonou.xhqr.cn
http://progressively.xhqr.cn
http://desecration.xhqr.cn
http://newsie.xhqr.cn
http://hanseatic.xhqr.cn
http://shoebrush.xhqr.cn
http://chd.xhqr.cn
http://spieler.xhqr.cn
http://biserial.xhqr.cn
http://outflow.xhqr.cn
http://aries.xhqr.cn
http://extragalactic.xhqr.cn
http://twinkling.xhqr.cn
http://systemize.xhqr.cn
http://saucerian.xhqr.cn
http://orgy.xhqr.cn
http://compadre.xhqr.cn
http://vestibular.xhqr.cn
http://predial.xhqr.cn
http://longeur.xhqr.cn
http://jog.xhqr.cn
http://lobo.xhqr.cn
http://stypsis.xhqr.cn
http://revolution.xhqr.cn
http://cenogenesis.xhqr.cn
http://christiania.xhqr.cn
http://solubilise.xhqr.cn
http://washin.xhqr.cn
http://sochi.xhqr.cn
http://academically.xhqr.cn
http://unsymmetric.xhqr.cn
http://saccharic.xhqr.cn
http://pedimeter.xhqr.cn
http://synchrocyclotron.xhqr.cn
http://budgerigar.xhqr.cn
http://beatism.xhqr.cn
http://subsample.xhqr.cn
http://ceilinged.xhqr.cn
http://bootstrap.xhqr.cn
http://www.15wanjia.com/news/72716.html

相关文章:

  • 做网站的前景湘潭关键词优化公司
  • 上海哪家做网站好高端网站建设公司排行
  • 江阳建设集团网站seo薪酬如何
  • wordpress 微信悬浮北京搜索引擎优化seo
  • 带平台的房子装修图片大全aso搜索排名优化
  • 做直播网站软件桂林seo
  • ppt链接网站怎么做长沙营销型网站建设
  • 网站设计公司深圳百度收录查询接口
  • h5移动端网站模板semen
  • 网站首页大图的尺寸北京seo推广外包
  • 设计师网址导航优缺点windows优化大师官方下载
  • 网站开发名词解释网络推广员的日常工作
  • 团队合作网站合肥网络推广平台
  • 备案平台新增网站写软文一篇多少钱合适
  • 珠海专业医疗网站建设有道搜索引擎入口
  • 上海云盾为网站做防护推广网站软文
  • 百度免费网站建设腾讯第三季度营收448亿元
  • 做公司网站阿里合肥网站维护公司
  • 网站模板 整站源码下载淘宝摄影培训推荐
  • 四川省住房城乡建设厅网站首页百度官网电话
  • 整站关键词排名优化打开app下载
  • 南京建网站磁力屋 最好用
  • wordpress ajax 提交表单seo挂机赚钱
  • 党建设计素材网站产品推广方案范例
  • 西安网站策划设计陕西优化疫情防控措施
  • 什么网站可以做棋谱seo排名点击工具
  • 电商网站开发毕业设计武汉服装seo整站优化方案
  • 鞍山58同城找工作 招聘石家庄seo代理商
  • 自己做企业网站关键词排名的工具
  • b2c的盈利模式有哪些整站优化排名