leetcod的tfind返回错误的指针
tfind查询成功后,按照manual手册说明,应该返回树节点的指针。但是,leetcode的tfind返回的不是指针。见代码的81行。
已经提交defect给leetcode,期待leetcode尽快修复,下次考试还要用呢:)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <securec.h>
#define C_CLASS(...) 1
#define ERR 1
#define OK 0
#define VAL(a, x, y) ((a[x])[y])
#define MVAL(a, m, i, j) ((a)[(i) * (m) + (j)])
#define __DBG__
#ifdef __DBG__
#define DBG(fmt, argv...) do { \
(void)printf(fmt, ##argv); \
} while (0)
#else
#define DBG(...)
#endif
#if (C_CLASS("solution"))
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <search.h>
int Cmp(const void *a0, const void *b0)
{
int a = *(int *)a0;
int b = *(int *)b0;
return a - b;
}
void NoFree(void *a)
{
return;
}
int findMaximumXOR(int *num, int ns)
{
if (ns <= 1) {
return 0;
}
int i;
unsigned int max = (unsigned int)num[0];
for (i = 1; i < ns; i++) {
max = MAX(max, num[i]);
}
int len = 0;
for (i = 31; i > -1; i--) {
if ((unsigned int)max & ((unsigned int)1 << i)) { // 计算最高bit位
len = i + 1;
break;
}
}
int maxor = 0;
int curor = 0;
int j;
int *prelist = (int *)malloc(ns * sizeof(int));
int idx = 0;
for (i = len - 1; i >= 0; i--) {
maxor <<= 1;
curor = maxor | 1;
void *root = NULL;
idx = 0;
for (j = 0; j < ns; j++) {
int p = num[j] >> i;
prelist[idx] = p; // 把前缀加入到list
if (tsearch(prelist + idx, &root, Cmp)) { // 把前缀所在list地址加入到二叉树
idx++; // 加入成功则list的个数加1
}
}
for (j = 0; j < idx; j++) {
int p = prelist[j];
int p1 = curor ^ p;
int *fptr = (int *)tfind(&p1, &root, Cmp);
if (fptr) {
printf("line:%u, fptr=%p\n",__LINE__, fptr);
//if (*fptr) {
maxor = curor;
//}
}
}
tdestroy(root, NoFree); // NoFree不释放任何内存,由list来释放
}
free(prelist); // 释放list
return maxor;
}
#endif
#ifdef __DEBUG__
#include "421.testcase.h"
#endif