算法题常见的BUG错误(总结)

1. 快排的partition

if(l >= r)
    return ;
int i = l, j = r;
int tmp = v[i];
while(i < j) {
    while(i < j && v[j] >= tmp) j--;
    while(i < j && v[i] <= tmp) i++;
    if(i < j) swap(v[i], v[j]);
}
swap(v[l], v[i]);

2. pq的排序规则

pop正好是和比较是反着的

struct cmp{
    bool operator ()(const pair<int,pair<int,int>> &a, const pair<int,pair<int,int>> &b) {
        return a.first > b.first;
    }
};

3. kmp的next数组

next[0] = -1;
for(int i=1; i<len; i++) {
    int j = next[i-1];
    while(j != -1 && s[j+1] != s[i])
        j = next[j];
    if(s[j+1] == s[i])
        next[i] = j+1;
    else 
        next[i] = -1;
}
posted @ 2019-06-08 13:55 Draymonder 阅读(...) 评论(...) 编辑 收藏