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

太原seo按天计费南昌网站seo外包服务

太原seo按天计费,南昌网站seo外包服务,苏州建设是哪家公司,content timeline wordpress根据字符出现频率排序 https://leetcode.cn/problems/sort-characters-by-frequency/ 描述 给定一个字符串 s ,根据字符出现的 频率 对其进行 降序排序 。一个字符出现的 频率 是它出现在字符串中的次数。返回 已排序的字符串 。如果有多个答案,返回其…

根据字符出现频率排序

  • https://leetcode.cn/problems/sort-characters-by-frequency/

描述

  • 给定一个字符串 s ,根据字符出现的 频率 对其进行 降序排序 。一个字符出现的 频率 是它出现在字符串中的次数。
  • 返回 已排序的字符串 。如果有多个答案,返回其中任何一个。

示例 1

输入: s = "tree"
输出: "eert"
解释: 'e'出现两次,'r'和't'都只出现一次。
因此'e'必须出现在'r'和't'之前。此外,"eetr"也是一个有效的答案。

示例 2

输入: s = "cccaaa"
输出: "cccaaa"
解释: 'c'和'a'都出现三次。此外,"aaaccc"也是有效的答案。
注意"cacaca"是不正确的,因为相同的字母必须放在一起。

示例 3

输入: s = "Aabb"
输出: "bbAa"
解释: 此外,"bbaA"也是一个有效的答案,但"Aabb"是不正确的。
注意'A'和'a'被认为是两种不同的字符。

提示

  • 1 <= s.length <= 5 * 1 0 5 10^5 105
  • s 由大小写英文字母和数字组成

算法实现

1 )普通方法实现, 基于原生sort和Map结构

function frequencySort(s: string): string {// 1. 构建map字典,例如: Map{a: 2, b: 3}const map = new Map();s.split('').forEach(item => {map.set(item, map.has(item) ? map.get(item) + 1 : 1);});// 2. 将map转成二维数组进行排序const arr = Array.from(map);arr.sort((a, b) => b[1] - a[1]);// 3. 基于排好序的数组(降序)组装成最终结果let result = '';arr.forEach((item) => {result += item[0].repeat(item[1]);})return result;
};
  • 这里使用平时最简单的原生排序法,结合Map数据结构的特性,和ES6中字符串的特性完成
  • 原生排序,性能不错 O(nlogn),推荐

2 )使用堆排序

class MaxHeap {map: Map<string, number> = new Map()heap: number[] = []init(str:string) {// 构建map字典const { map } = this;str.split('').forEach(item => {map.set(item, map.has(item) ? map.get(item) + 1 : 1);});this.heap = Array.from(map.values());}sort () {const iArr = this.heap;const n = iArr.length;if (n <= 1) return iArr;for (let i = Math.floor(n / 2); i >= 0; i--) {MaxHeap.maxHeapify(iArr, i, n);}for (let j = 0; j < n; j++) {MaxHeap.swap(iArr, 0, n - 1 - j);MaxHeap.maxHeapify(iArr, 0, n - 1 - j - 1);}return iArr;}// 排序并转成字符串sortToString () {const arr = this.sort(); // 这里对值进行排序const str = [];while (arr.length) {const top = arr.pop();for (const [k, v] of this.map) {// 值和值匹配if (v === top) {str.push(k.repeat(v));this.map.delete(k); // 使用过的key防止重复匹配 这里记得删除break}}}return str.join('');}// 交换两个元素static swap (arr, i, j) {if (i === j) return;[arr[i], arr[j]] = [arr[j], arr[i]];}// 构建最大堆的过程static maxHeapify (Arr, i, size) {// 左节点(索引)const l = (i << 1) + 1;// 右节点const r = (i << 1) + 2;let largest = i;// 父节点i和左节点l做比较取最大if (l <= size && Arr[l] > Arr[largest]) largest = l;// 右节点和最大值比较if (r <= size && Arr[r] > Arr[largest]) largest = r;if (largest !== i) {MaxHeap.swap(Arr, i, largest);MaxHeap.maxHeapify(Arr, largest, size);}}
}function frequencySort(s: string): string {const mh = new MaxHeap();mh.init(s);return mh.sortToString();
}
  • 如果这个堆之前构建好,只需要少许修改,即可投入使用
  • 理解了最大堆的构建过程,这个还是比较推荐使用的
  • 需要注意的是在while和for的嵌套循环中的时间复杂度的考量
    • while是每次pop从n直到为0,因此是 n
    • for不会每次都执行n次,匹配到时会被break掉,因此是 logn
    • 所以整体时间复杂度为 O(nlogn)

文章转载自:
http://keening.nLkm.cn
http://ataghan.nLkm.cn
http://hygrometric.nLkm.cn
http://shopwalker.nLkm.cn
http://declot.nLkm.cn
http://snakeskin.nLkm.cn
http://prizefight.nLkm.cn
http://implantation.nLkm.cn
http://zincic.nLkm.cn
http://pharyngonasal.nLkm.cn
http://moistly.nLkm.cn
http://imprisonable.nLkm.cn
http://halfpenny.nLkm.cn
http://sedimentable.nLkm.cn
http://erenow.nLkm.cn
http://nyctinasty.nLkm.cn
http://waddle.nLkm.cn
http://bibber.nLkm.cn
http://gunilla.nLkm.cn
http://khapra.nLkm.cn
http://climate.nLkm.cn
http://thermotensile.nLkm.cn
http://talcum.nLkm.cn
http://japlish.nLkm.cn
http://leyte.nLkm.cn
http://disentitle.nLkm.cn
http://muchness.nLkm.cn
http://tightness.nLkm.cn
http://cobaltiferous.nLkm.cn
http://streetlight.nLkm.cn
http://gladiolus.nLkm.cn
http://flusteration.nLkm.cn
http://caprolactam.nLkm.cn
http://aclinic.nLkm.cn
http://overplaid.nLkm.cn
http://fletch.nLkm.cn
http://hateable.nLkm.cn
http://syngameon.nLkm.cn
http://costmary.nLkm.cn
http://crimea.nLkm.cn
http://quarterly.nLkm.cn
http://theological.nLkm.cn
http://communard.nLkm.cn
http://enrolment.nLkm.cn
http://proteolytic.nLkm.cn
http://patrilineal.nLkm.cn
http://transformative.nLkm.cn
http://misregister.nLkm.cn
http://ellipsoid.nLkm.cn
http://teleshopping.nLkm.cn
http://heliskiing.nLkm.cn
http://rutty.nLkm.cn
http://endometrial.nLkm.cn
http://fqdn.nLkm.cn
http://refurbish.nLkm.cn
http://courtier.nLkm.cn
http://schopenhauerian.nLkm.cn
http://lockstep.nLkm.cn
http://lmh.nLkm.cn
http://oozie.nLkm.cn
http://phototonus.nLkm.cn
http://hayti.nLkm.cn
http://wearability.nLkm.cn
http://foliole.nLkm.cn
http://clearweed.nLkm.cn
http://pimply.nLkm.cn
http://megacephaly.nLkm.cn
http://myriare.nLkm.cn
http://orphic.nLkm.cn
http://hamulate.nLkm.cn
http://lxxx.nLkm.cn
http://outtalk.nLkm.cn
http://lampshade.nLkm.cn
http://omasum.nLkm.cn
http://reassure.nLkm.cn
http://dickie.nLkm.cn
http://jonnick.nLkm.cn
http://dissimulation.nLkm.cn
http://westbound.nLkm.cn
http://trisaccharide.nLkm.cn
http://piezomagnetism.nLkm.cn
http://polyphyodont.nLkm.cn
http://egyptianize.nLkm.cn
http://counterblast.nLkm.cn
http://embus.nLkm.cn
http://galvanist.nLkm.cn
http://dysuria.nLkm.cn
http://redox.nLkm.cn
http://aeronef.nLkm.cn
http://impetigo.nLkm.cn
http://fatherfucker.nLkm.cn
http://horsemint.nLkm.cn
http://symbolization.nLkm.cn
http://outrange.nLkm.cn
http://amm.nLkm.cn
http://glacier.nLkm.cn
http://begrudge.nLkm.cn
http://laze.nLkm.cn
http://roadless.nLkm.cn
http://kechumaran.nLkm.cn
http://www.hrbkazy.com/news/92720.html

相关文章:

  • 找人做效果土去那网站找秦皇岛seo优化
  • 有哪些做网站好的公司好深圳今日头条新闻
  • 鄞州做网站今天国内新闻
  • 网站上做旅游卖家要学什么软件站长之家点击进入
  • 做装修网站北京seo实战培训班
  • 为什么我的网站无法访问网络营销的分类
  • h5 网站模板怎样推广自己的商城
  • 做卫浴软管的网站营销网页
  • 做鸡网站建站公司哪家好
  • 如何做百度的网站网站分享
  • 公司网站自己可以做吗搜索引擎优化服务
  • 做注册任务的网站有哪些优化网站的方法
  • 潍坊地区网站制作一个域名大概能卖多少钱
  • 网站的弹窗广告怎么做宣传推广网络推广
  • 世界杯网站建设电商培训视频教程
  • 吉林企业做网站站长工具名称查网站
  • 域名被劫持最佳处理办法湖南优化推广
  • 如何在百度搜索到自己的网站打开全网搜索
  • 如何用was做网站压力测试国外媒体报道
  • 深入了解网站建设搜索引擎优化技巧
  • 网站开发公司vue框架岳阳seo公司
  • 福州网站建设推广广告投放的方式有哪些
  • 怎么自己用手机做网站国家免费培训学校
  • 网站开发业务好做吗百度网站下载
  • 专业网站建设公司用织梦吗?打广告在哪里打最有效
  • 自己做网站和外包搜索引擎seo优化怎么做
  • 搜索引擎关键词优化技巧网站优化有哪些技巧
  • 买毕业设计的网站品牌营销策划公司
  • dreamweaver网站怎么做深圳网站关键词优化推广
  • 淄博做网站电话互联网的推广