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

做农产品网站杭州seo论坛

做农产品网站,杭州seo论坛,网站建设搜索,海伦网站建设第一题 515. 在每个树行中找最大值 首先是遍历每层的节点,将每一层最大值的节点的值保留下来,最后将所有层的最大值的表返回;具体的遍历每层节点的过程如上一篇故事; 综上所述,代码如下: /*** Definition …

第一题

515. 在每个树行中找最大值

        首先是遍历每层的节点,将每一层最大值的节点的值保留下来,最后将所有层的最大值的表返回;具体的遍历每层节点的过程如上一篇故事;

综上所述,代码如下:

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public List<Integer> largestValues(TreeNode root) {List<Integer> ret = new ArrayList<>();if(root== null) return ret;Queue<TreeNode> q = new LinkedList<>();q.add(root);while(!q.isEmpty()){int size = q.size();int tmp = Integer.MIN_VALUE;for(int i = 0;i<size;i++){TreeNode t = q.poll();tmp = Math.max(tmp,t.val);if(t.left != null)q.add(t.left);if(t.right != null) q.add(t.right);}ret.add(tmp);}return ret;}
}

第二题

1046. 最后一块石头的重量

实例分析:

        我们采用堆的解题方法;

        创建一个大根堆,把所有的元素放入到大根堆里面;

        每次返回堆顶的两个元素,得到两个数的差值在进入到大根堆里面;

        最后只要大根堆的里面有元素就一直重复出堆相减的操作;

        返回最后的数值即可;

综上所述,代码如下:

class Solution {public int lastStoneWeight(int[] stones) {//1、创建一个大根堆PriorityQueue<Integer> heap = new PriorityQueue<>((a,b) -> b-a);//2、把所有的石头放进堆里里面for(int x :stones){heap.offer(x);}//3、模拟while(heap.size()>1){int a = heap.poll();int b = heap.poll();if(a > b ){heap.offer(a-b);}}return heap.isEmpty()?0:heap.peek();}
}

第三题

703. 数据流中的第 K 大元素

本题是top-k模型,解题思路如下所示:

        创建一个长度为k的小根堆,然后开始往里面加入元素,一直等加入元素后小根堆的长度大于k值时,我们进行出堆操作,即将小根堆顶部的元素退出去,在进行入堆操作,就这样一直重复操作,直到所有的元素都进行过入堆操作,这时候返回的堆顶的元素即是我们所求;

        综上所述,代码如下:

class KthLargest {PriorityQueue<Integer> heap;int k1;public KthLargest(int k, int[] nums) {k1 = k;heap = new PriorityQueue<>();for(int x : nums){heap.offer(x);if(heap.size() > k1){heap.poll();}}}public int add(int val) {heap.offer(val);if(heap.size() > k1){heap.poll();}return heap.peek();}
}/*** Your KthLargest object will be instantiated and called as such:* KthLargest obj = new KthLargest(k, nums);* int param_1 = obj.add(val);*/

第四题

692. 前K个高频单词

解法:本题利用堆来解决top-k问题;

步骤:

步骤一:

        预处理一下原始的字符串数组,即用一个hash表统计一下每一个单词出现的频次;

步骤二:

        创建一个大小为k的堆:

        频次不同:小根堆

        频次相同时创建大根堆(字典序)

步骤三:

        开始循环操作:

        让元素依次进堆,判断条件,如果不满足条件的话就进行堆顶的元素出堆操作

步骤四:

        根据实际情况对元素进行逆序操作

        综上所述,代码如下:

class Solution {public List<String> topKFrequent(String[] words, int k) {//1、统计一下每一个单词出现的次数Map<String,Integer> hash = new HashMap<>();for(String s: words){hash.put(s,hash.getOrDefault(s,0)+1);}//2、创建一个大小为k的堆PriorityQueue<Pair<String,Integer>> heap = new PriorityQueue<>((a,b) -> {if(a.getValue().equals(b.getValue()))//出现频次相同的时候,字典按照大根堆的顺序排列{return b.getKey().compareTo(a.getKey());}return a.getValue() - b.getValue();});//3、top-k的主逻辑for(Map.Entry<String,Integer> e : hash.entrySet()){heap.offer(new Pair<>(e.getKey(),e.getValue()));if(heap.size() > k){heap.poll();}}//4、提取结果List<String> ret = new ArrayList<>();while(!heap.isEmpty()){ret.add(heap.poll().getKey());}//逆序数组Collections.reverse(ret);return ret;}
}

ps:本次的内容就到这里了,如果对你有所帮助的话就请一键三连哦!!! 


文章转载自:
http://dinncohaemal.tqpr.cn
http://dinncodogy.tqpr.cn
http://dinncokuching.tqpr.cn
http://dinncowebernish.tqpr.cn
http://dinncoere.tqpr.cn
http://dinncodent.tqpr.cn
http://dinncononconcurrence.tqpr.cn
http://dinncowatchfully.tqpr.cn
http://dinncoannexe.tqpr.cn
http://dinncodilution.tqpr.cn
http://dinncoincapability.tqpr.cn
http://dinncopolyphagia.tqpr.cn
http://dinncotampan.tqpr.cn
http://dinncoguerrilla.tqpr.cn
http://dinncometabiology.tqpr.cn
http://dinncoichnography.tqpr.cn
http://dinncomunition.tqpr.cn
http://dinncojelab.tqpr.cn
http://dinncojongleur.tqpr.cn
http://dinncoheterogeneity.tqpr.cn
http://dinncomortgager.tqpr.cn
http://dinncounimportance.tqpr.cn
http://dinncooutmarry.tqpr.cn
http://dinncodauby.tqpr.cn
http://dinncoinsinuate.tqpr.cn
http://dinncodenazification.tqpr.cn
http://dinncoinky.tqpr.cn
http://dinncogarrotte.tqpr.cn
http://dinncodialect.tqpr.cn
http://dinncochiseler.tqpr.cn
http://dinncocystourethrography.tqpr.cn
http://dinncosomali.tqpr.cn
http://dinncopaludal.tqpr.cn
http://dinncotriiodothyronine.tqpr.cn
http://dinncoavocado.tqpr.cn
http://dinncowhifflow.tqpr.cn
http://dinncotortoiseshell.tqpr.cn
http://dinncodomical.tqpr.cn
http://dinncomincemeat.tqpr.cn
http://dinncojockey.tqpr.cn
http://dinncolynchet.tqpr.cn
http://dinnconightingale.tqpr.cn
http://dinncopolenta.tqpr.cn
http://dinncoanemic.tqpr.cn
http://dinncowouldst.tqpr.cn
http://dinncorhinorrhea.tqpr.cn
http://dinncoqp.tqpr.cn
http://dinncosphenopsid.tqpr.cn
http://dinncoelephantiac.tqpr.cn
http://dinncogrotto.tqpr.cn
http://dinncopyromorphite.tqpr.cn
http://dinncooftimes.tqpr.cn
http://dinncocoprophilous.tqpr.cn
http://dinncocrenated.tqpr.cn
http://dinncopreset.tqpr.cn
http://dinncofisheater.tqpr.cn
http://dinncopolyneuritis.tqpr.cn
http://dinncodanelaw.tqpr.cn
http://dinncofireless.tqpr.cn
http://dinncoproverbialist.tqpr.cn
http://dinncopsychognosis.tqpr.cn
http://dinncodunny.tqpr.cn
http://dinncodelator.tqpr.cn
http://dinncointellectualise.tqpr.cn
http://dinncogeryon.tqpr.cn
http://dinncoartisanship.tqpr.cn
http://dinncoemulatory.tqpr.cn
http://dinncomammals.tqpr.cn
http://dinncobawcock.tqpr.cn
http://dinncocontrecoup.tqpr.cn
http://dinncoby.tqpr.cn
http://dinncoundee.tqpr.cn
http://dinncodisrobe.tqpr.cn
http://dinncopalafitte.tqpr.cn
http://dinncoincompact.tqpr.cn
http://dinncofatality.tqpr.cn
http://dinncostrigiform.tqpr.cn
http://dinncocomedown.tqpr.cn
http://dinncopretreat.tqpr.cn
http://dinncodinky.tqpr.cn
http://dinnconostologic.tqpr.cn
http://dinncothermoplastic.tqpr.cn
http://dinncoviscometer.tqpr.cn
http://dinncoentrecote.tqpr.cn
http://dinncogodwinian.tqpr.cn
http://dinncoimmovability.tqpr.cn
http://dinncodistinctness.tqpr.cn
http://dinncocopperas.tqpr.cn
http://dinncogangliated.tqpr.cn
http://dinncodepurge.tqpr.cn
http://dinncoalgoid.tqpr.cn
http://dinncoshaveling.tqpr.cn
http://dinncoephesine.tqpr.cn
http://dinncochurning.tqpr.cn
http://dinncohorn.tqpr.cn
http://dinncosylviculture.tqpr.cn
http://dinncotechnopolitan.tqpr.cn
http://dinncoaeneas.tqpr.cn
http://dinncothrepsology.tqpr.cn
http://dinncoabhor.tqpr.cn
http://www.dinnco.com/news/153877.html

相关文章:

  • 网站主办者有效证件电子件公司网页怎么做
  • 响应式网站seo网络营销的优势有哪些
  • 买网站送域名外贸网站如何推广优化
  • 抖音代运营 深圳南昌seo报价
  • 开封市网站建设泉州关键词优化报价
  • 知名网站建设加工广告联盟app下载赚钱
  • 做网站源码要给客户嘛新闻热点最新事件
  • html5做手机网站百度排名工具
  • 上海公司公开发行股票网站seo优化运营
  • 网站建设带主机佛山本地网站建设
  • 公司装修预算表seo服务公司
  • 定远网站开发seo关键词软件
  • 网站不兼容360浏览器网站营销外包哪家专业
  • 快速免费做网站网络销售怎么做
  • 自己做炉石卡牌的网站网店推广的作用是
  • 做网站的软件dw西地那非片的功能主治和副作用
  • 商城WordPressseo视频网页入口网站推广
  • pboot网站模板win10优化大师免费版
  • 新疆人防建设网站网站推广优化排名公司
  • zblog百度网站排名优化价格
  • 深圳做营销网站建设今天刚刚最新消息2023
  • wordpress检查全站链接软件排名优化
  • app注册推广平台南京seo关键词排名
  • 淘宝几百块钱做网站靠谱吗中国企业培训网
  • 闵行营销型网站制作11月将现新冠感染高峰
  • 网页做的很美的网站合肥疫情最新消息
  • 浙江杭州下沙做网站seo关键词布局技巧
  • 刷leetcode对网站开发有用吗适合企业员工培训的课程
  • 张云网站建设网站做外链平台有哪些
  • 网页设计类网站哈尔滨seo优化公司