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

Golang | Leetcode Golang题解之第451题根据字符出现频率排序

题目:

题解:

func frequencySort(s string) string {cnt := map[byte]int{}maxFreq := 0for i := range s {cnt[s[i]]++maxFreq = max(maxFreq, cnt[s[i]])}buckets := make([][]byte, maxFreq+1)for ch, c := range cnt {buckets[c] = append(buckets[c], ch)}ans := make([]byte, 0, len(s))for i := maxFreq; i > 0; i-- {for _, ch := range buckets[i] {ans = append(ans, bytes.Repeat([]byte{ch}, i)...)}}return string(ans)
}func max(a, b int) int {if a > b {return a}return b
}

http://www.mrgr.cn/news/40957.html

相关文章:

  • 分布式共识算法ZAB
  • C++继承的三种方式[ACCESS]
  • AI学习指南深度学习篇-权重正则化的基本原理
  • 在C语言中,符号有两个主要用途:
  • 水污染水质检测数据集 2400张 水污染 带标注 voc yolo 4类
  • Linux 磁盘管理
  • 【架构】prometheus+grafana系统监控
  • Python | Leetcode Python题解之第450题删除二叉搜索树中的节点
  • C++网络编程之IP地址和端口
  • 实战OpenCV之轮廓检测
  • SQL Server—表格详解
  • SpringBoot开发——Spring Security中获取当前登录用户信息的方式
  • 读数据湖仓04数据架构与数据工程
  • CountDownLatch、CyclicBarrier、Semaphore等并发工具类的使用。JVM内存区域划分(堆、栈、方法区、程序计数器、本地方法栈)。
  • 某大型公园定岗定编项目成功案例纪实
  • 【架构】efk日志监控
  • 开放式耳机是什么意思?哪个品牌好?开放式蓝牙耳机测评分享
  • WaterCloud:一套基于.NET 8.0 + LayUI的快速开发框架,完全开源免费!
  • Stable Diffusion绘画 | 来训练属于自己的模型:打标处理与优化
  • Python 3 和 MySQL(PyMySQL) 的完美结合