-1
我在Leetcode OJ上做了“按频率排序字符”问题。 任何人都可以向我解释为什么会发生这种情况?为什么超出内存限制?
class Solution {
public:
struct Node {
int freq;
char ch;
};
static bool lambda(struct Node a, struct Node b){
return a.freq>b.freq;
}
string frequencySort(string s) {
if(s.size() == 0)
return "";
string res = "";
vector<Node> nums(256);
for(char c : s){
nums[(int)c].ch = c;
nums[(int)c].freq++;
}
std::sort(nums.begin(), nums.end(), Solution::lambda);
char c;
for(int i=0; nums[i].freq > 0; i++){
c = nums[i].ch;
while(nums[i].freq--){
res = res + c; // If I replace this line with res += c, it gets Accepted!
}
}
return res;
}
};
我们需要足够的代码来复制问题。 –
你了解'res = res + c;'和'res + = c;'的区别吗? –
更多信息。 – Sean83