Leetcode 1366:堆缓冲区溢出
Leetcode 1366: Heap Buffer Overflow
这是我的 Leetcode 1366 代码,它不适用于某些测试用例,例如 ["XYZ","XZY"]
我对算法非常确定,即使用字符对和向量的向量作为映射。在我看来,代码有问题,而不是算法。
class Solution {
public:
string rankTeams(vector<string>& votes) {
int n = votes.size();
int m = votes[0].length();
if(n==1) return votes[0];
if(m==1) return votes[0];
string temp = votes[0];
sort(temp.begin(), temp.end());
vector<pair<char,vector<int>>> map(m);
vector<int> vec(m,0);
for(int i=0;i<m;i++){
map[i].first = temp[i];
map[i].second = vec;
}
//initialize the map
for(int i=0;i<n;i++){ //pick a voter
for(int j=0;j<m;j++){ //pick votes
map[votes[i][j] - 'A'].second[j]++;
}
}
sort(map.begin(), map.end(), [&](pair<char,vector<int>> pair1, pair<char,vector<int>> pair2){
for(int pos=0;pos<m;pos++){
if((pair1.second)[pos] > (pair2.second)[pos]) return true;
else if((pair1.second)[pos] < (pair2.second)[pos]) return false;
//else if((pair1.second)[pos] == (pair2.second)[pos]) continue;
}
return pair1.first < pair2.first;
});
string result;
for(int i=0;i<m;i++) result[i] = map[i].first;
return result;
}
};
特别是,错误消息说 -
Runtime Error
=================================================================
==33==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x608000000308 at pc 0x00000038d06a bp 0x7ffe00621720 sp 0x7ffe00621718
READ of size 8 at 0x608000000308 thread T0
#4 0x7f57283cd82f (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
Address 0x608000000308 is a wild pointer.
Shadow bytes around the buggy address:
0x0c107fff8010: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c107fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c107fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c107fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c107fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
=>0x0c107fff8060: fa[fa]fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c107fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c107fff8080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c107fff8090: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c107fff80a0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c107fff80b0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
Shadow gap: cc
==33==ABORTING
这个问题的"在线判断"中有一个错误,我想 LeetCode 应该会修复它。除此之外,以下解决方案是公认的解决方案,并且要简单得多:
class Solution {
public:
string rankTeams(vector<string> &votes) {
vector<vector<int>> count_map(26, vector<int>(27));
for (char &character : votes[0])
count_map[character - 65][26] = character;
for (string &vote : votes)
for (int index = 0; index < vote.length(); index++)
count_map[vote[index] - 65][index]--;
sort(count_map.begin(), count_map.end());
string sorted_teams;
for (int index = 0; index < votes[0].length(); index++)
sorted_teams += count_map[index][26];
return sorted_teams;
}
};
如果您要面试,请确保编写具有一致风格的干净代码。
<小时 />参考资料
- 有关其他详细信息,您可以查看讨论区。其中有很多公认的解决方案、解释、多种语言的高效算法以及时间/空间复杂性分析。
- ASCII 图表
- 1366.按选票对团队进行排名
- 1366. 按投票对团队进行排名 - 讨论区
相关文章:
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 为什么我在leetcode上收到AddressSanitizer:地址0x602000000058上的堆缓冲区溢出错误
- 在 leetcode 上提交解决方案时出现堆栈缓冲区溢出错误
- 使用 strcat 获取缓冲区溢出错误
- LeetCode 1:两和 - 地址清理器:堆缓冲区溢出地址
- 使用向量的缓冲区溢出
- 重新增长阵列时出错:写入时缓冲区溢出
- Leetcode 1366:堆缓冲区溢出
- 缓冲区溢出 - 数组索引越界(严重)
- C++二维矢量导致缓冲区溢出
- 缓冲区溢出怎么会成为黑客的攻击
- 如何防止缓冲区溢出
- 应该如何读取堆缓冲区溢出错误消息?
- 自动截断和 null 终止缓冲区溢出中的字符串缓冲区
- 如果用户输入两个或多个由空格分隔的字符串C++如何防止缓冲区溢出?
- 我正在尝试使用回溯来解决 N queen 问题,但在编译时它会给出运行时错误(动态堆栈缓冲区溢出)
- 为什么 LeetCode 给出错误:地址清理器:堆缓冲区溢出
- 为什么错误 C6386 缓冲区溢出与 strsafe.h 字符串 Cch 函数
- 读取文件时字符缓冲区溢出
- C 文本写入随机数据.是否有缓冲区溢出