【问题标题】:Segmentation fault in LIS solution for Cracking the Coding interview 11.7Cracking the Coding interview 11.7 LIS 解决方案中的分段错误
【发布时间】:2015-05-15 13:19:23
【问题描述】:

破解编码面试出现问题-part V Question 11.7 我在这里尝试实现的解决方案是他们在 Java 中转换为 C++ 的解决方案。但我面临分段错误的问题。

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;
class HtWt
{
    public:
        int ht;
        int wt;
        HtWt(int ht,int wt):ht(ht),wt(wt){}
        HtWt(const HtWt &other)
        {
            this->ht=other.ht;
            this->wt=other.wt;
        }
        bool operator<(const HtWt& obj) const
        {
            cout << __func__ << std::endl;
            return (this->ht<obj.ht && this->wt<obj.wt);
        }
};

typedef vector<HtWt> vHtWt;
typedef vector<vHtWt > vvHtWt;

vHtWt& getSeqWithMaxLen(vHtWt& seq1,vHtWt& seq2)
{
    cout << __func__ << std::endl;
    if(seq1.empty())
        return seq2;
    if(seq2.empty())
        return seq1;
    return (seq1.size() > seq2.size() ? seq1 : seq2);
}

void  LIS(vHtWt& arr,vvHtWt& solutions,int current_index)
{
    cout << __func__ << std::endl;
    if(current_index>arr.size()-1 || current_index<0)
        return;
    cout<<"arr.size()="<<arr.size()<<"current_index = "<<current_index<<endl;
    HtWt cur_element = arr[current_index];
    /* Find longest sequence we can append current_element to */
    vHtWt best_sequence;
    for(int i=0;i<current_index;i++)
    {
        cout<<"inside for loop"<<endl;
        if (arr[i]<cur_element)
            best_sequence = getSeqWithMaxLen(best_sequence,solutions[i]);
            //cout<<"{"<<best_sequence[best_sequence.size()-1].ht<<","<<best_sequence[best_sequence.size()-1].wt<<"}"<<" ";
    }
    /* Append current_element */
    vHtWt new_solution;
    if(!best_sequence.empty())
        new_solution.insert(new_solution.end(),best_sequence.begin(),best_sequence.end());
    new_solution.push_back(cur_element);  
    /* Add to list and recurse */
    solutions[current_index] = new_solution;
    LIS(arr,solutions,current_index+1);
}

vHtWt LIS(vHtWt& arr)
{
    cout << __func__ << std::endl;
    vvHtWt solutions;
    LIS(arr,solutions,0);
    vHtWt best_sequence;
    for(int i=0;i<arr.size();i++)
        best_sequence = getSeqWithMaxLen(best_sequence,solutions[i]);
    return best_sequence;
}

vHtWt getLIS(vHtWt& arr)
{
    cout << __func__ << std::endl;
    // sort the array for either height or weight 
    sort(arr.begin(),arr.end());
    return LIS(arr);
}

int main()
{
    HtWt arr[] = {HtWt(12, 13), HtWt(11, 15), HtWt(9, 20), HtWt(20, 20), HtWt(40, 21), HtWt(8, 42)};
    vHtWt vArr(arr,arr+(sizeof(arr)/sizeof(arr[0])));
    vHtWt result = getLIS(vArr);
    for(int i=0;i<result.size();i++)
        cout<<"{"<<result[i].ht<<","<<result[i].wt<<"}"<<" ";
    cout<<endl;
    return 0;
}

谁能帮我解释一下为什么代码在行崩溃

HtWt cur_element = arr[current_index];  

arr 的大小是 6 , index 是 0 ,所以我猜应该不存在分段错误。

$ ./a.out 
getLIS
operator<
operator<
operator<
operator<
operator<
LIS
LIS
arr.size()=6current_index = 0
Segmentation fault: 11

【问题讨论】:

  • 看起来像堆栈溢出(因为涉及到递归)。你能把bt贴在这里吗?想看@堆栈深度,即帧数。

标签: c++ algorithm segmentation-fault lis


【解决方案1】:

您没有在线路上遇到分段错误

HtWt cur_element = arr[current_index]; 但在

solutions[current_index] = new_solution;

因为solutions 尚未初始化。换行

vvHtWt solutions;到

vvHtWt solutions(arr.size());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    • 2014-08-19
    • 2010-11-29
    相关资源
    最近更新 更多