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

网络推广专员要求seo 推广服务

网络推广专员要求,seo 推广服务,个人域名备案需要哪些资料,纯净水企业怎样做网站"你了解我,最干净的轮廓, 握住小小风车和放肆的梦~" 堆是一个不错的数据结构,而在计算机中,无法表示二叉分支结构,因此我们经常会看到使用线性表来作为堆的存储容器。在接触堆的时候,我们是把它…

"你了解我,最干净的轮廓, 握住小小风车和放肆的梦~" 


        堆是一个不错的数据结构,而在计算机中,无法表示二叉分支结构,因此我们经常会看到使用线性表来作为堆的存储容器。在接触堆的时候,我们是把它拿来同其他排序算法来看待的,但其实我们经常使用的是快排或者归并亦或者性能更加优越的"选择快排"。堆的应用场景,实质上转移到了查找问题,例如TopK等。很多语言也提供了以堆为底层的数据结构,例如C++中的priority_queue,Java中的PriorityQueue……

如何实现一个堆排序?

        我们对任意一个堆的定义是一个完全二叉树,当一个父节点的值,大于左右子节点的值,则称为大堆,反之如果一个父节点的值,小于左右节点的值,则被称之为小堆。

        建堆的方法有两种,一种是"向上调整法",一种是"向下调整法"。前者的思想是着眼于整个数组,后者的思想着眼于分治,先将最小的子树进行一次堆排序,再不停向上迭代。因为"向下调整法"实现起来更为简单,并且效率更高,所以我们选择后者。

(1) 构建一个堆(大堆)

// 找到最后的父节点
for (int i = (n - 1 - 1 ) / 2;i >= 0;--i)
{adjustDown(nums, i);
}void adjustDown(vector<int>& nums,int parent)
{// 控制下标int n = nums.size();int child = parent * 2 + 1;while (child < n){// 把更大的换上去if (child+1 < n && nums[child] < nums[child + 1]) child++;// 比较if (nums[parent] < nums[child]) {swap(nums[parent], nums[child]);// 更新parent = child;child = parent * 2 + 1;}else {// 结束break;}}
}

(2) 堆排序

        要实现"升序排序,构建大堆,反之构建小堆"(因为本篇不是着眼于堆排序,不解释)。

void adjustDown(vector<int>& nums,int parent,int end)
{// 控制下标int child = parent * 2 + 1;while (child < end){// 把更大的换上去if (child+1 < end && nums[child] < nums[child + 1]) child++;// 比较if (nums[parent] < nums[child]) {swap(nums[parent], nums[child]);// 更新parent = child;child = parent * 2 + 1;}else {// 结束break;}}
}void HeapSort(vector<int>& nums)
{// 建堆int n = nums.size();// 找到最后的父节点for (int i = (n - 1 - 1 ) / 2;i >= 0;--i){adjustDown(nums, i,n);}// 排序int end = n - 1;while (end > 0){// 因为构建的是大堆,所有后面的数一定是小数swap(nums[end], nums[0]); // 同堆顶交换 让最大的数 在最后一个adjustDown(nums, 0,end);// 排序好一个数end--;}for (auto& n : nums)cout << n << " ";cout << endl;
}

——前言


1、最后一块石头的重量

(1) 题目解析        

(2) 算法原理        

C++: 

class Solution {
public:int lastStoneWeight(vector<int>& stones) {// 寻找大数,构建大堆priority_queue<int,vector<int>,less<int>> heap;// 入队列for(auto& n:stones) heap.push(n);// 模拟出队列while(heap.size() > 1){int x = heap.top();heap.pop();int y = heap.top();heap.pop();// 插入差值heap.push(x-y);}return heap.size() == 0 ? 0:heap.top();}
};

Java:

class Solution {public int lastStoneWeight(int[] stones) {PriorityQueue<Integer> heap = new PriorityQueue<>((a, b) -> b - a);for(int n:stones) heap.offer(n);// 模拟while(heap.size() > 1){int a = heap.poll();int b= heap.poll();heap.offer(a-b);}return heap.isEmpty() ? 0 : heap.peek();}
}

2、数据流中的第K大元素

(1) 题目解析

        这也是一个经典的topK问题,对于要找第K大的数,我们需要构建是小堆,而不是大堆。反之,要查找第K小的数,我们需要构建的是大堆,而不是小堆。

(2) 算法原理

c++: 

class KthLargest {
public:// 构建小堆priority_queue<int,vector<int>,greater<int>> _heap;int _k; // 构建多大的kKthLargest(int k, vector<int>& nums) {_k = k;// 入队列for(auto& n:nums){_heap.push(n);if(_heap.size() > _k) _heap.pop(); // 剔除多余数}}int add(int val) {_heap.push(val);if(_heap.size() > _k) _heap.pop();return _heap.top();}
};

 java:

class KthLargest {PriorityQueue<Integer> heap;int _k;public KthLargest(int k, int[] nums) {_k = k;heap = new PriorityQueue<>(); // 默认是小堆for(int x:nums){heap.offer(x);if(heap.size() > _k) heap.poll();}}public int add(int val) {heap.offer(val);if(heap.size() > _k) heap.poll();return heap.peek();}
}

3、前K个高频单词

(1) 题目解        

(2) 算法原理   

class Solution {
public:typedef pair<int,string> PSI; // 频次与字符串 用于比较struct cmp{template<class T>bool operator()(T& t1,T& t2){return (t1.first > t2.first) || (t1.first == t2.first) && (t1.second < t2.second);}};vector<string> topKFrequent(vector<string>& words, int k) {unordered_map<string,int> hash; // 统计频次for(auto& str:words) hash[str]++;// 普通的比较函数: less\greater 不能满足我们的要求// 所以我们得更换比较函数: 这里我们采用的是 仿函数priority_queue<PSI,vector<PSI>,cmp> heap;for(auto& n:hash){heap.push({n.second,n.first});if(heap.size() > k) heap.pop();}// 倒序vector<string> ret(k);for(int i=k-1;i>=0;--i){ret[i] = heap.top().second;heap.pop();}return ret;}
};


4、数据流中的中位数

(1) 题目解析        

        前两者解法都是很好想的,前提就是保证数组有序,再来找中位数。但,我们这里选择的解法不是其中的任意一种。

(2) 算法原理        

class MedianFinder {
public:priority_queue<int,vector<int>,less<int>> _left;priority_queue<int,vector<int>,greater<int>> _right;MedianFinder() {}void addNum(int num) {if(_left.size() == _right.size()){if(_left.empty() || num <= _left.top()){// 直接进入_left.push(num);}else{// 替换_right.push(num);_left.push(_right.top());_right.pop();}}else{if(num <= _left.top()){_left.push(num);_right.push(_left.top());_left.pop();}else{_right.push(num);}}}double findMedian() {if(_left.size() == _right.size()) return (_left.top() + _right.top()) / 2.0;return _left.top(); // m=n+1}
};

本篇到此结束,感谢你的阅读。

祝你好运,向阳而生~


文章转载自:
http://dinncoaddendum.tqpr.cn
http://dinncongwane.tqpr.cn
http://dinncopatras.tqpr.cn
http://dinncobotb.tqpr.cn
http://dinncopendent.tqpr.cn
http://dinncosilversides.tqpr.cn
http://dinncoclosely.tqpr.cn
http://dinncodigamma.tqpr.cn
http://dinncocringingly.tqpr.cn
http://dinncoametoecious.tqpr.cn
http://dinncosureshot.tqpr.cn
http://dinncogranita.tqpr.cn
http://dinncowelterweight.tqpr.cn
http://dinncokinetosis.tqpr.cn
http://dinncofictitious.tqpr.cn
http://dinncospanworm.tqpr.cn
http://dinncopalmaceous.tqpr.cn
http://dinncohooray.tqpr.cn
http://dinncotungstite.tqpr.cn
http://dinncocrystallogram.tqpr.cn
http://dinncoventriculostomy.tqpr.cn
http://dinncoestablishmentarian.tqpr.cn
http://dinncopatio.tqpr.cn
http://dinncolazarette.tqpr.cn
http://dinncochuckwalla.tqpr.cn
http://dinncoemblema.tqpr.cn
http://dinncointerchangeable.tqpr.cn
http://dinncospectropolarimeter.tqpr.cn
http://dinncotyphomania.tqpr.cn
http://dinncosamite.tqpr.cn
http://dinncomisdeed.tqpr.cn
http://dinncoofficialism.tqpr.cn
http://dinncohemophiliac.tqpr.cn
http://dinncodeific.tqpr.cn
http://dinncoanticaries.tqpr.cn
http://dinncoalabandite.tqpr.cn
http://dinncodextrorotatory.tqpr.cn
http://dinncoallure.tqpr.cn
http://dinncowindstick.tqpr.cn
http://dinncocaravansarai.tqpr.cn
http://dinncodendrology.tqpr.cn
http://dinncoandrocentric.tqpr.cn
http://dinncoigo.tqpr.cn
http://dinncotetrodotoxin.tqpr.cn
http://dinncosootfall.tqpr.cn
http://dinncocytogamy.tqpr.cn
http://dinnconomothetic.tqpr.cn
http://dinncohimalayan.tqpr.cn
http://dinncoconfabulator.tqpr.cn
http://dinncoswordsmanship.tqpr.cn
http://dinncoincompletely.tqpr.cn
http://dinncouncultured.tqpr.cn
http://dinncohubby.tqpr.cn
http://dinncowayfarer.tqpr.cn
http://dinncoprocumbent.tqpr.cn
http://dinncoresorption.tqpr.cn
http://dinncopontifex.tqpr.cn
http://dinncopolybasite.tqpr.cn
http://dinncotauten.tqpr.cn
http://dinncoleftwards.tqpr.cn
http://dinncomuseum.tqpr.cn
http://dinncopongid.tqpr.cn
http://dinncothew.tqpr.cn
http://dinncodeodorant.tqpr.cn
http://dinncoperceptual.tqpr.cn
http://dinncotentatively.tqpr.cn
http://dinncodie.tqpr.cn
http://dinncoswimgloat.tqpr.cn
http://dinnconaled.tqpr.cn
http://dinncoism.tqpr.cn
http://dinncoparaphysics.tqpr.cn
http://dinncocurtilage.tqpr.cn
http://dinncounlooked.tqpr.cn
http://dinncohousewares.tqpr.cn
http://dinncoogive.tqpr.cn
http://dinncocoagulatory.tqpr.cn
http://dinncosulfid.tqpr.cn
http://dinnconodosity.tqpr.cn
http://dinncoexophthalmos.tqpr.cn
http://dinncoclabularium.tqpr.cn
http://dinncodandle.tqpr.cn
http://dinncorenogram.tqpr.cn
http://dinncotendinitis.tqpr.cn
http://dinncomontgolfier.tqpr.cn
http://dinnconodous.tqpr.cn
http://dinncoinworks.tqpr.cn
http://dinncodram.tqpr.cn
http://dinncocentroid.tqpr.cn
http://dinncobone.tqpr.cn
http://dinncohostler.tqpr.cn
http://dinncolawyerly.tqpr.cn
http://dinncofimbria.tqpr.cn
http://dinncoskid.tqpr.cn
http://dinncorpi.tqpr.cn
http://dinncogalactosan.tqpr.cn
http://dinncounconsciousness.tqpr.cn
http://dinncosneering.tqpr.cn
http://dinncorubicundity.tqpr.cn
http://dinncoretreat.tqpr.cn
http://dinncocompensatory.tqpr.cn
http://www.dinnco.com/news/159493.html

相关文章:

  • 石家庄网站建设价格佛山网络推广公司
  • 网站类型的销售网站推广应该怎么做?
  • 两个网站链接怎么做微营销
  • 微官网与网站的区别奖券世界推广网站
  • 东莞房价2023最新价格南宁seo公司哪家好
  • 做网站网页的软件是绿色的图标什么手游推广个人合作平台
  • 福州seo关键词排名seo教程网站优化推广排名
  • 濉溪建设投资网站网站 软件
  • 荣成市建设局网站是什么网站建设制作过程
  • 政府网站建设管理方面工作总结百度知道个人中心
  • 鄂州网站制作销售平台
  • 免费旅行社网站模板嘉兴新站seo外包
  • 网站开发人员 工资竞价推广怎样管理
  • 如何用div和css做购物网站bt磁力种子搜索引擎
  • 搜索引擎排名网站漯河网站seo
  • 网站后台样式设计案例网
  • 长春企业网站排名优化广告代理商
  • logo设计网站在线长清区seo网络优化软件
  • 开网站卖茶要怎么做如何引流与推广
  • 东营新闻联播在线直播今晚宁波seo快速优化课程
  • 编辑网站内容怎么做滚动图片电商还有发展前景吗
  • 九江网站建设推广长春网站制作推广
  • 临沂做网站wyjzgzs中国疫情最新消息
  • 手机网站测试互动营销用在哪些推广上面
  • 网站建设需要哪些知识杭州百度推广代理公司哪家好
  • 绚丽网站模板新榜数据平台
  • 网站开发报价表的文档工具刷网站排刷排名软件
  • 软装设计培训班哪家好长沙seo网站优化
  • 深圳做公司网站的公司1688官网入口
  • 网站推广优化开发建设手机百度一下