当前位置:网站首页>leetcode: 358. Reorder strings at K distance intervals
leetcode: 358. Reorder strings at K distance intervals
2022-08-11 02:47:00 【OceanStar's study notes】
题目来源
题目描述
class Solution {
public:
string rearrangeString(string str, int k) {
}
};
题目解析
思路:
- 准备:
- 先统计所有字符出现的次数,存入一个map中(key = char, value = count)
- 然后使用mapBuild a large root heap according to the number of repetitions from largest to smallest
- 创建一个string ansObjects are used to represent results
- 创建一个queueto sync has been added toans中的字符
- Loop through the large root heap of characters
- The characters with the most numbers are inserted first,The number of characters after insertion-1,Then move the character to queue中(因为下一次不能插入它了,Have to wait until the length arriveskOnly then can it be inserted)
- 然后判断queue中的元素是否为k,如果是的话,Indicates that the element at the head of the queue can be tried again to insert,So put it into the big root pile
- 最后,When the big root heap is empty:
- 如果ans.size == ori.sizeDescription Refactoring is complete,返回ans
- 如果ans.size != ori.size,The description also has some characters hanging onqueue中,重构失败,返回""
class Solution {
public:
string rearrangeString(string str, int k) {
if(k == 0){
return str;
}
int N = str.size();
std::string ans;
std::unordered_map<char, int> m;
// 遍历字符,统计字符的出现次数
for(auto c : str){
m[c]++;
}
// 装入大顶堆,按照字符重复次数作为比较
std::priority_queue<pair<char, int>, std::vector<pair<char, int>>,
cmp> maxHeap(m.begin(), m.end());
std::queue<pair<char, int>> queue;
while (!maxHeap.empty()){
auto curr = maxHeap.top(); maxHeap.pop();// 从大顶堆取出重复次数最多的字符
ans += curr.first;
curr.second--;// 用掉一个字符,次数减一
queue.push(curr);// 放入到queue中,因为k距离后还要用
if(queue.size() == k){
// queue的大小到达了k,也就是说我们已经越过了k个单位,在结果中应该要出现相同的字母了
auto f = queue.front(); queue.pop();
if(f.second > 0){
// 该字符的重复次数大于 0,则添加入大顶堆中,要是0那还加它干嘛
maxHeap.push(f);
}
}
}
return ans.size() == str.size() ? ans : "";
}
};
类似题目
- leetcode:358. K 距离间隔重排字符串
- leetcode:621. Task Scheduler
- leetcode:767. 重构字符串(堆)
边栏推荐
猜你喜欢
【oops-framework】模板项目【oops-game-kit】使用简介
redis学习五redis的持久化RDB,fork,copyonwrite,AOF,RDB&AOF混合使用
Detailed explanation of new features of ES advanced array function syntax
Idea (优选)cherry-pick操作
How to solve the problem of Tomcat booting and crashing
多线程之ThreadPoolExecutor
正式发布丨VS Code 1.70
CSAPP Data Lab
OpenCV创始人:开源绝不能完全免费!
ESP32的环境配置(arduino arduino2.0 VScode platform哪个好用?)
随机推荐
gRPC闭包调度器
创业的第125天——随记
js中的this问题
①In-depth analysis of CAS SSO single sign-on framework source code
2022年G1工业锅炉司炉题库及模拟考试
117. 本地开发好的 SAP UI5 应用部署到 ABAP 服务器时,中文字符变成乱码的原因分析和解决方案
【Unity入门计划】Unity2D动画(1)-动画系统的组成及功能的使用
0图中等 LeetCode565. 数组嵌套
重庆纸质发票再见!开住宿费电子发票即将全面取代酒店餐饮加油站发票
Vim and copy and paste from the outside (don't need to install the plugin)
shell [stdin/stdout/stderr][重定向]
CC0 vs. commercial IP: which model is better for NFTs?
混币器Tornado遭制裁 对DeFi市场意味着什么?
[idea error] Invalid target distribution: 17 solution reference
“京台高铁”亮相百度地图,真能在2035年建成吗?
YTU 2418: C语言习题 矩阵元素变换
qtcreator调试webkit
全局大喇叭--广播机制
调试技巧总结
2022年广东省安全员A证第三批(主要负责人)操作证考试题模拟考试平台操作