@@ -59599,35 +59599,34 @@ https://leetcode.cn/problems/sort-characters-by-frequency 的多语言解法👇
59599
59599
class Solution {
59600
59600
public:
59601
59601
string frequencySort(string s) {
59602
- char[] chars = s.toCharArray( );
59602
+ vector< char> chars(s.begin(), s.end() );
59603
59603
// s 中的字符 -> 该字符出现的频率
59604
59604
unordered_map<char, int> charToFreq;
59605
59605
for (char ch : chars) {
59606
- charToFreq[ch] = charToFreq[ch] + 1 ;
59606
+ charToFreq[ch]++ ;
59607
59607
}
59608
59608
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 );
59614
59614
59615
59615
// 按照字符频率排序
59616
- for (const auto& entry : charToFreq) {
59616
+ for (auto& entry : charToFreq) {
59617
59617
pq.push(entry);
59618
59618
}
59619
59619
59620
- string res;
59620
+ string res = "" ;
59621
59621
while (!pq.empty()) {
59622
59622
// 把频率最高的字符排在前面
59623
59623
pair<char, int> entry = pq.top();
59624
59624
pq.pop();
59625
- string part(entry.second, entry.first);
59626
- res.append(part);
59625
+ res += string(entry.second, entry.first);
59627
59626
}
59628
59627
59629
59628
return res;
59630
- }
59629
+ }
59631
59630
};
59632
59631
```
59633
59632
@@ -67673,6 +67672,146 @@ class Codec:
67673
67672
67674
67673
https://leetcode.cn/problems/xu-lie-hua-er-cha-shu-lcof 的多语言解法👆
67675
67674
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
+
67676
67815
https://leetcode.cn/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof 的多语言解法👇
67677
67816
67678
67817
```cpp
0 commit comments