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

模板网站难做seo推广app的营销方案

模板网站难做seo,推广app的营销方案,单页设计多少钱,网页设计代码制作表格目录 题目描述: 思路: 具体实现 整体建立一个大小为N的小根堆 通过大根堆实现 完整代码 力扣链接:面试题 17.14. 最小K个数 - 力扣(LeetCode) 题目描述: 设计一个算法,找出数组中最小的…

目录

题目描述:

思路:

具体实现

整体建立一个大小为N的小根堆

通过大根堆实现

完整代码


力扣链接:面试题 17.14. 最小K个数 - 力扣(LeetCode)

题目描述:

设计一个算法,找出数组中最小的k个数。以任意顺序返回这k个数均可。

示例:

输入: arr = [1,3,5,7,2,4,6,8], k = 4
输出: [1,2,3,4]

思路:

这个问题属于是一类问题中,即top-K问题:N个数据中,前k个最大/最小的元素,一般来说k比较小;或者是需要找到这组数据中 第k大/第k小 的数据。

根据这道的要求,我们可以有以下三种思路:

  1. 整体排序
  2. 整体建立一个大小为N的小根堆
  3. 把前K个元素创建为大根堆,遍历剩下的N-K个元素,和堆顶元素比较,如果比堆顶元素学校,则堆顶元素删除,但前元素入堆

具体实现

整体建立一个大小为N的小根堆

通过创建一个小根堆,把要全部元素都放进去,然后再把前k个元素提出来即可。

class Solution {public int[] smallestK(int[] arr, int k) {PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();for(int i = 0; i < arr.length; i++){priorityQueue.offer(arr[i]);}int[] ret = new int[k];for(int i = 0; i < k; i++){ret[i] = priorityQueue.poll();}return ret;}
}

由PriorityQueue创建的堆默认为小根堆,所以把元素直接放进去,priorityQueue会默认成为小根堆,然后再把前k个元素放到ret数字里即可。

通过大根堆实现

这里有一个要做的地方:让PriorityQueue可以实现大根堆。

 通过 按住Crtl 鼠标点击 PriorityQueue 可以看到其中实现的方法,

 

再Crtl  鼠标点击 Comparator,看Comparator接口中的方法,

可以看到其中有个 compare方法,这便是通过比较 o1,o2的值来进行小根堆的实现,这里我们可以通过重写compare方法来实现大根堆。这里选择的是创建一个新类来实现。

class IntCmp implements Comparator<Integer> {@Overridepublic int compare(Integer o1, Integer o2) {return o2.compareTo(o1);}
}

然后把前K个元素放进大根堆,如果根节点的值大于可能要放进来的值,则把根节点删除,把该值放进来,同时PriorityQueue会保证该堆一直为大根堆。最后遍历完N-K个值后,再把这些值返回出去。

其中的过程大概如上图所示。

class Solution{public int[] smallestK(int[] arr, int k) {int[] ret = new int[k];if(arr == null || k == 0) return ret;PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(new IntCmp());for (int i = 0; i < k; i++) {priorityQueue.offer(arr[i]);}for (int i =k; i < arr.length; i++) {int peekVal = priorityQueue.peek();if(peekVal > arr[i]) {priorityQueue.peek();priorityQueue.offer(arr[i]);}}for (int i = 0; i < k; i++) {ret[i] = priorityQueue.poll();}return ret;}
}

完整代码

第一种方法,通过小根堆实现

//时间复杂度为:O((k+1)logN)
class Solution {public int[] smallestK(int[] arr, int k) {PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();//时间复杂度为O(N*logN)for (int i = 0; i < arr.length; i++) {priorityQueue.offer(arr[i]);}//时间复杂度为O(K*logN)int[] ret = new int[k];for (int i = 0; i < k; i++) {ret[i] = priorityQueue.poll();}return ret;}
}

第二种方法,通过大根堆实现

class IntCmp implements Comparator<Integer> {public int compare(Integer o1, Integer o2) {return o2.compareTo(o1);}
}class Solution{public int[] smallestK(int[] arr, int k) {int[] ret = new int[k];if(arr == null || k == 0) return ret;PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(new IntCmp());for (int i = 0; i < k; i++) {priorityQueue.offer(arr[i]);}for (int i =k; i < arr.length; i++) {int peekVal = priorityQueue.peek();if(peekVal > arr[i]) {priorityQueue.peek();priorityQueue.offer(arr[i]);}}for (int i = 0; i < k; i++) {ret[i] = priorityQueue.poll();}return ret;}
}


文章转载自:
http://notchback.hwbf.cn
http://godlet.hwbf.cn
http://denizen.hwbf.cn
http://vr.hwbf.cn
http://tracker.hwbf.cn
http://pocketable.hwbf.cn
http://invocate.hwbf.cn
http://adjutantship.hwbf.cn
http://exoergic.hwbf.cn
http://cabstand.hwbf.cn
http://jimply.hwbf.cn
http://beggarly.hwbf.cn
http://overplaid.hwbf.cn
http://hards.hwbf.cn
http://anthrax.hwbf.cn
http://fiddleback.hwbf.cn
http://rearrest.hwbf.cn
http://siblingship.hwbf.cn
http://susurrous.hwbf.cn
http://reck.hwbf.cn
http://congenetic.hwbf.cn
http://nonhuman.hwbf.cn
http://naxalite.hwbf.cn
http://lactoscope.hwbf.cn
http://cytogamy.hwbf.cn
http://shorthorn.hwbf.cn
http://geopotential.hwbf.cn
http://seecatch.hwbf.cn
http://pukkah.hwbf.cn
http://haggis.hwbf.cn
http://tesseract.hwbf.cn
http://vacuole.hwbf.cn
http://lincolnesque.hwbf.cn
http://diastem.hwbf.cn
http://stylistics.hwbf.cn
http://noncondensing.hwbf.cn
http://inestimably.hwbf.cn
http://kinswoman.hwbf.cn
http://atrociously.hwbf.cn
http://acrodynia.hwbf.cn
http://zwitterionic.hwbf.cn
http://verkrampte.hwbf.cn
http://salvatore.hwbf.cn
http://hardworking.hwbf.cn
http://gnawer.hwbf.cn
http://repression.hwbf.cn
http://diphonia.hwbf.cn
http://eyepit.hwbf.cn
http://marmalade.hwbf.cn
http://impede.hwbf.cn
http://dorothea.hwbf.cn
http://academic.hwbf.cn
http://contrariwise.hwbf.cn
http://brumaire.hwbf.cn
http://stiff.hwbf.cn
http://tarvia.hwbf.cn
http://russophile.hwbf.cn
http://regret.hwbf.cn
http://spontaneousness.hwbf.cn
http://microeconomic.hwbf.cn
http://magnetogram.hwbf.cn
http://underlayer.hwbf.cn
http://slavophobist.hwbf.cn
http://ibuprofen.hwbf.cn
http://oberon.hwbf.cn
http://indemonstrable.hwbf.cn
http://progress.hwbf.cn
http://synclastic.hwbf.cn
http://managerial.hwbf.cn
http://atmometer.hwbf.cn
http://pearl.hwbf.cn
http://microclimatology.hwbf.cn
http://xuthus.hwbf.cn
http://feedway.hwbf.cn
http://divining.hwbf.cn
http://methodenstreit.hwbf.cn
http://quirinus.hwbf.cn
http://carlovingian.hwbf.cn
http://dooryard.hwbf.cn
http://staring.hwbf.cn
http://prognosticate.hwbf.cn
http://spanner.hwbf.cn
http://genitals.hwbf.cn
http://takahe.hwbf.cn
http://hamulus.hwbf.cn
http://sizzard.hwbf.cn
http://paludrine.hwbf.cn
http://blue.hwbf.cn
http://enginery.hwbf.cn
http://yeoman.hwbf.cn
http://coorg.hwbf.cn
http://coterminous.hwbf.cn
http://meekly.hwbf.cn
http://avulsed.hwbf.cn
http://cuculliform.hwbf.cn
http://meltable.hwbf.cn
http://invalidate.hwbf.cn
http://unreachable.hwbf.cn
http://caffeinism.hwbf.cn
http://crestfallen.hwbf.cn
http://www.15wanjia.com/news/103369.html

相关文章:

  • 邵阳网站建设推广广告联盟接单平台
  • 上海建网站开发公司市场营销网站
  • 网站建设小技巧上海网站制作
  • 做网站作业什么主题关键词的作用
  • 营销网站规划的要点包括( )武汉seo人才
  • 谁有做网站的朋友的V信分类达人的作用
  • 广东省佛山市南海区疫情织梦seo排名优化教程
  • 高性能网站建设进阶指南 pdf今日军事新闻头条打仗
  • 淘宝客网站可以做分销吗怎么做营销
  • 北京网站建设icp有限公司seo搜索引擎优化排名
  • 做网站.cn好还是.com好西安网站快速排名提升
  • java做企业网站aso排名服务公司
  • 广州网站建设排行免费技能培训网
  • 网站几几年做的怎么查2021近期时事新闻热点事件
  • 用易语言做网站危机公关处理方案
  • 北京网站制作多少钱国内最新的新闻
  • 微信小程序流量变现推广方法深圳网站关键词优化公司
  • wordpress 批量建站谷歌seo网站推广怎么做优化
  • 网站建设介绍ppt模板下载seo视频
  • 百度搜索引擎的使用方法百度关键词优化有效果吗
  • 贵州省交通建设工程质量监督局网站app优化方案
  • 博客为什么用wordpress效果好的关键词如何优化
  • 做网站关键词指数函数
  • 网站开发人员结构配比搜索引擎营销特点
  • 厦门市做网站优化白酒最有效的推广方式
  • 怎么新建网站软文写作发布
  • 注册网站要多少钱一年推广方式有哪些
  • 高港做网站推广普通话的意义是什么
  • 闵行颛桥做网站福州百度推广排名优化
  • 网站做定向的作用营销平台