请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

215题:波波老师,为什么我的代码clion运行正确,而在leetcode提交失败

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
,错误原因:Line 9: execution reached the end of a value-returning function without returning a value
class Solution {
public:
    int findKthLargest(vector<int>& nums, int k) {
     
        return quickSort(nums , 0 , nums.size()-1 ,nums.size() - k);
    }      
     
    int quickSort(vector<int>& nums ,int l ,int r ,int t ){
         
        int p = partition(nums ,l ,r );
        if(t == p)
            return nums[p];
        else if(t < p)
            quickSort(nums , l ,p-1 , t);
        else
            quickSort(nums, p+1 , r ,t);
    }
 
    int partition(vector<int>& nums ,int l ,int r){
     
        int v = nums[l];
        int i = l+1,j = l;//[l+1,j]保存<v的元素
        for(; i <= r; i++)
            if(nums[i] < v)
                swap(nums[i],nums[++j]);
        swap(nums[l], nums[j]);
     
        return j;
    }
};


正在回答

插入代码

2回答

这个报错是你的代码quickSort只有t == p的时候有返回值,在其他情况下没有返回值哦:)


在你的本地,由于编译器版本的不同,没有给出错误提示(实际上C++的编译器是出了名的检查不严格,所以很容易写出有bug的代码:)),不过被Leetcode的编译器检查出来了哦:)


我现在已经在我的leetcode上加上了这个问题的代码,如果有必要可以参考:)传送门:https://github.com/liuyubobobo/Play-Leetcode/blob/master/0215-Kth-Largest-Element-in-an-Array/cpp-0215/main.cpp


加油!:)

1 回复 有任何疑惑可以回复我~
提问者 慕沐9313579 2018-06-15 15:47:17

波波老师,您leetcode上关于215题的代码好像删除了

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号