Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from labuladong:master #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 1 commit into AlgorithmAndLeetCode:master from labuladong:master
Apr 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions 多语言解法代码/solution_code.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -59599,35 +59599,34 @@ https://leetcode.cn/problems/sort-characters-by-frequency 的多语言解法👇
class Solution {
public:
string frequencySort(string s) {
char[] chars = s.toCharArray();
vector<char> chars(s.begin(), s.end());
// s 中的字符 -> 该字符出现的频率
unordered_map<char, int> charToFreq;
for (char ch : chars) {
charToFreq[ch] = charToFreq[ch] + 1;
charToFreq[ch]++;
}

priority_queue<pair<char, int>, vector<pair<char, int>>, function<bool(pair<char, int>, pair<char, int>)>>
pq([](const pair<char, int>& entry1, const pair<char, int>& entry2) -> bool {
// 队列按照键值对中的值(字符出现频率)从大到小排序
return entry2.second < entry1.second;
});
auto cmp = [](pair<char, int>& entry1, pair<char, int>& entry2) {
return entry1.second < entry2.second;
};
// 队列按照键值对中的值(字符出现频率)从大到小排序
priority_queue<pair<char, int>, vector<pair<char, int>>, decltype(cmp)> pq(cmp);

// 按照字符频率排序
for (const auto& entry : charToFreq) {
for (auto& entry : charToFreq) {
pq.push(entry);
}

string res;
string res = "";
while (!pq.empty()) {
// 把频率最高的字符排在前面
pair<char, int> entry = pq.top();
pq.pop();
string part(entry.second, entry.first);
res.append(part);
res += string(entry.second, entry.first);
}

return res;
}
}
};
```

Expand Down

AltStyle によって変換されたページ (->オリジナル) /