讨论社区

C语言,页面执行代码和提交,对于相同用例返回不同的结果

在提交33. 搜索旋转排序数组时,通过页面上的代码执行相同的用例结果可以正确通过。但是正式提交时,提示栈溢出。为什么相同的代码,会有不同的相同的结果。



int start = 0;

int last = 0;


int findhalf(int *nums, int num) {

if (start == last) {

if (nums[start] == num) {

return start;

}

else {

return -1;

}

}

else {

int half = (start + last) / 2;

if (nums[half] == num) {

return half;

}

else if (nums[half] < num) {

start = half + 1;

return findhalf(nums, num);

}

else {

last = half - 1;

return findhalf(nums, num);

}

}

}


int findmax(int *nums, int num) {

int half = (start + last) / 2;

if (nums[half] < num) {

last = half - 1;

return findmax(nums, num);

}

return half;

}


int findmin(int *nums, int num) {

int half = (start + last) / 2;

if (nums[half] > num) {

start = half + 1;

return findmin(nums, num);

}

return half;

}





int search(int* nums, int numsSize, int target) {

last = numsSize - 1;

if (nums[start] < target) {

int flag = findmax(nums, nums[start]);

if (target == -1) {

return -1;

}

else if (target == nums[flag]) {

return flag;

}

else if (target > nums[flag]) {

start = flag;

return search(nums, last + 1, target);

}

else {

last = flag;

return findhalf(nums, target);

}

}else if (nums[start] > target) {

int flag = findmin(nums, nums[start]);

if (target == -1) {

return -1;

}else if (target == nums[flag]) {

return flag;

}

else if (target < nums[flag]) {

last = flag;

return search(nums, last + 1, target);

}

else {

start = flag;

return findhalf(nums, target);

}

}

}


0 人关注了该问题 关注

0

776854974m • 2月前

希望这篇内容对你有帮助:https://leetcode.kf5.com/hc/kb/article/1194344/

0 个讨论

您需要登录后才可回复
1

konghouyin • 2月前

错误反馈,是因为全局变量的问题吗

0 个讨论

您需要登录后才可回复
您需要登录后才可以回复