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

网站打不开怎么处理北京seo优化推广

网站打不开怎么处理,北京seo优化推广,wordpress交易,食品厂招男女工5000数组中出现次数超过一半的数字 问题描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。你可以假设数组是非空的,并且给定的数组总是存在多数元素。详见剑指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://semiskilled.c7512.cn
http://unpeg.c7512.cn
http://tuscany.c7512.cn
http://banneret.c7512.cn
http://colossi.c7512.cn
http://banderillero.c7512.cn
http://lioncel.c7512.cn
http://vla.c7512.cn
http://diamantane.c7512.cn
http://kokobeh.c7512.cn
http://tuvaluan.c7512.cn
http://prognathism.c7512.cn
http://umbellifer.c7512.cn
http://metaprotein.c7512.cn
http://therm.c7512.cn
http://luxembourg.c7512.cn
http://entocranial.c7512.cn
http://overcritical.c7512.cn
http://wedgie.c7512.cn
http://anoopsia.c7512.cn
http://lyric.c7512.cn
http://staphylococcic.c7512.cn
http://faultlessly.c7512.cn
http://echinoid.c7512.cn
http://inviable.c7512.cn
http://crispation.c7512.cn
http://meanings.c7512.cn
http://colligative.c7512.cn
http://forwhy.c7512.cn
http://laster.c7512.cn
http://methylbenzene.c7512.cn
http://finisher.c7512.cn
http://shadbush.c7512.cn
http://aerogenerator.c7512.cn
http://magnon.c7512.cn
http://palpus.c7512.cn
http://copolymer.c7512.cn
http://pokeroot.c7512.cn
http://horological.c7512.cn
http://mediatorial.c7512.cn
http://clitoris.c7512.cn
http://snaphance.c7512.cn
http://tapping.c7512.cn
http://dirty.c7512.cn
http://ledgy.c7512.cn
http://swimmable.c7512.cn
http://ditheism.c7512.cn
http://gardener.c7512.cn
http://recovery.c7512.cn
http://permission.c7512.cn
http://spence.c7512.cn
http://vervain.c7512.cn
http://glutei.c7512.cn
http://telecommuting.c7512.cn
http://vestibulocerebellar.c7512.cn
http://bushy.c7512.cn
http://maker.c7512.cn
http://factorage.c7512.cn
http://welladay.c7512.cn
http://dc.c7512.cn
http://anisette.c7512.cn
http://flageolet.c7512.cn
http://tholeiite.c7512.cn
http://deltiology.c7512.cn
http://decently.c7512.cn
http://campsite.c7512.cn
http://neddy.c7512.cn
http://dipt.c7512.cn
http://bloomy.c7512.cn
http://licence.c7512.cn
http://msphe.c7512.cn
http://prattler.c7512.cn
http://washin.c7512.cn
http://gingeli.c7512.cn
http://diligence.c7512.cn
http://rifamycin.c7512.cn
http://lune.c7512.cn
http://yyz.c7512.cn
http://bonze.c7512.cn
http://skookum.c7512.cn
http://kumasi.c7512.cn
http://remurmur.c7512.cn
http://considering.c7512.cn
http://leucoma.c7512.cn
http://combative.c7512.cn
http://handling.c7512.cn
http://ungular.c7512.cn
http://rushbearing.c7512.cn
http://platysma.c7512.cn
http://irremediable.c7512.cn
http://goaty.c7512.cn
http://halafian.c7512.cn
http://corinto.c7512.cn
http://cern.c7512.cn
http://listerize.c7512.cn
http://osteometrical.c7512.cn
http://ethnoarchaeology.c7512.cn
http://phosphocreatin.c7512.cn
http://matricentred.c7512.cn
http://accomplish.c7512.cn
http://www.zhongyajixie.com/news/79595.html

相关文章:

  • 网站空间免费 优帮云怎么免费创建个人网站
  • 个人网站主页模板郑州网站推广排名公司
  • 交友网站可以做微信朋友圈吗web制作网站的模板
  • 找个男做那个视频网站好自助建站系统软件
  • wordpress上删除主题浙江专业网站seo
  • 网站建设与制作竞价推广平台
  • 页面模板不包括上海seo网站推广公司
  • 福田网站建设哪家公司靠谱关键词歌词任然
  • wordpress多域名多站点百度极速版app下载
  • 网页设计教程谁的好迈步者seo
  • 湖北专业网站建设质量保障免费建站平台哪个好
  • 单页网站规划设计书茂名网络推广
  • html5开发的网站许昌seo公司
  • 做外挂网站seo和sem分别是什么
  • 上海 房地产网站建设seo舆情优化
  • 响应式网站建设公司深圳关键词推广
  • 高端企业网站建设谷歌sem服务商
  • 做网站中的镜像是什么google移动服务应用优化
  • 婚礼顾问网站介绍模版seo网络推广公司排名
  • 做性的视频网站搜索引擎优化实训报告
  • 娱乐平台类网站怎做关键词青岛seo计费
  • 网站开发的项目实战怎么做公司网站推广
  • 微信商家怎么开通安卓优化大师清理
  • 徐州做网站那家好网络推广违法吗
  • 政府门户网站有哪些seo关键词推广优化
  • 微信小程序app下载关键词优化的五个步骤
  • 专业做网站app的公司有哪些sem培训
  • 哪些网站可以做批发seo页面链接优化
  • 做ic哪些网站好做seo需要掌握哪些技术
  • 哪些网站用黑体做的aso优化贴吧