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

Commit 4d601b0

Browse files
update content
1 parent 1d1a7a1 commit 4d601b0

File tree

1 file changed

+151
-12
lines changed

1 file changed

+151
-12
lines changed

‎多语言解法代码/solution_code.md‎

Lines changed: 151 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59599,35 +59599,34 @@ https://leetcode.cn/problems/sort-characters-by-frequency 的多语言解法👇
5959959599
class Solution {
5960059600
public:
5960159601
string frequencySort(string s) {
59602-
char[] chars = s.toCharArray();
59602+
vector<char> chars(s.begin(), s.end());
5960359603
// s 中的字符 -> 该字符出现的频率
5960459604
unordered_map<char, int> charToFreq;
5960559605
for (char ch : chars) {
59606-
charToFreq[ch] = charToFreq[ch] + 1;
59606+
charToFreq[ch]++;
5960759607
}
5960859608

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

5961559615
// 按照字符频率排序
59616-
for (const auto& entry : charToFreq) {
59616+
for (auto& entry : charToFreq) {
5961759617
pq.push(entry);
5961859618
}
5961959619

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

5962959628
return res;
59630-
}
59629+
}
5963159630
};
5963259631
```
5963359632

@@ -67673,6 +67672,146 @@ class Codec:
6767367672

6767467673
https://leetcode.cn/problems/xu-lie-hua-er-cha-shu-lcof 的多语言解法👆
6767567674

67675+
https://leetcode.cn/problems/xx4gT2 的多语言解法👇
67676+
67677+
```cpp
67678+
// by chatGPT (cpp)
67679+
class Solution {
67680+
public:
67681+
int minSubArrayLen(int target, vector<int>& nums) {
67682+
int left = 0, right = 0;
67683+
// 维护窗口内元素之和
67684+
int windowSum = 0;
67685+
int res = INT_MAX;
67686+
67687+
while (right < nums.size()) {
67688+
// 扩大窗口
67689+
windowSum += nums[right];
67690+
right++;
67691+
while (windowSum >= target && left < right) {
67692+
// 已经达到 target,缩小窗口,同时更新答案
67693+
res = min(res, right - left);
67694+
windowSum -= nums[left];
67695+
left++;
67696+
}
67697+
}
67698+
return res == INT_MAX ? 0 : res;
67699+
}
67700+
};
67701+
```
67702+
67703+
```go
67704+
// by chatGPT (go)
67705+
import (
67706+
"fmt"
67707+
"math"
67708+
)
67709+
67710+
func minSubArrayLen(target int, nums []int) int {
67711+
left, right := 0, 0
67712+
// 维护窗口内元素之和
67713+
windowSum := 0
67714+
res := math.MaxInt64
67715+
67716+
for right < len(nums) {
67717+
// 扩大窗口
67718+
windowSum += nums[right]
67719+
right++
67720+
for windowSum >= target && left < right {
67721+
// 已经达到 target,缩小窗口,同时更新答案
67722+
res = min(res, right - left)
67723+
windowSum -= nums[left]
67724+
left++
67725+
}
67726+
}
67727+
if res == math.MaxInt64 {
67728+
return 0
67729+
}
67730+
return res
67731+
}
67732+
67733+
// 获取两个整数的最小值
67734+
func min(a, b int) int {
67735+
if a < b {
67736+
return a
67737+
}
67738+
return b
67739+
}
67740+
```
67741+
67742+
```java
67743+
// by labuladong (java)
67744+
class Solution {
67745+
public int findKthLargest(int[] nums, int k) {
67746+
// 小顶堆,堆顶是最小元素
67747+
PriorityQueue<Integer> pq = new PriorityQueue<>();
67748+
for (int e : nums) {
67749+
// 每个元素都要过一遍二叉堆
67750+
pq.offer(e);
67751+
// 堆中元素多于 k 个时,删除堆顶元素
67752+
if (pq.size() > k) {
67753+
pq.poll();
67754+
}
67755+
}
67756+
// pq 中剩下的是 nums 中 k 个最大元素,
67757+
// 堆顶是最小的那个,即第 k 个最大元素
67758+
return pq.peek();
67759+
}
67760+
}
67761+
```
67762+
67763+
```javascript
67764+
// by chatGPT (javascript)
67765+
/**
67766+
* @param {number} target
67767+
* @param {number[]} nums
67768+
* @return {number}
67769+
*/
67770+
var minSubArrayLen = function(target, nums) {
67771+
var left = 0, right = 0;
67772+
// 维护窗口内元素之和
67773+
var windowSum = 0;
67774+
var res = Number.MAX_SAFE_INTEGER;
67775+
67776+
while (right < nums.length) {
67777+
// 扩大窗口
67778+
windowSum += nums[right];
67779+
right++;
67780+
while (windowSum >= target && left < right) {
67781+
// 已经达到 target,缩小窗口,同时更新答案
67782+
res = Math.min(res, right - left);
67783+
windowSum -= nums[left];
67784+
left++;
67785+
}
67786+
}
67787+
return res == Number.MAX_SAFE_INTEGER ? 0 : res;
67788+
};
67789+
```
67790+
67791+
```python
67792+
# by chatGPT (python)
67793+
class Solution:
67794+
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
67795+
left = 0
67796+
right = 0
67797+
# 维护窗口内元素之和
67798+
windowSum = 0
67799+
res = sys.maxsize
67800+
67801+
while right < len(nums):
67802+
# 扩大窗口
67803+
windowSum += nums[right]
67804+
right += 1
67805+
while windowSum >= target and left < right:
67806+
# 已经达到 target,缩小窗口,同时更新答案
67807+
res = min(res, right - left)
67808+
windowSum -= nums[left]
67809+
left += 1
67810+
return res if res != sys.maxsize else 0
67811+
```
67812+
67813+
https://leetcode.cn/problems/xx4gT2 的多语言解法👆
67814+
6767667815
https://leetcode.cn/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof 的多语言解法👇
6767767816

6767867817
```cpp

0 commit comments

Comments
(0)

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