terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_M_construct null not valid


  • 0
    J

    my visual studio can run it,but when I submit on the platform,it throw out the error:
    terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_M_construct null not valid
    what can I do?Who could help me?


  • 0
    J

    my program is :
    class Solution {
    public:
    string longestPalindrome(string s) {
    string b;
    string ans;
    int len = 0,cas=0;
    for (int j = s.length() - 1; j >0; j--)
    {
    int k = j;
    int flag = 0;
    int i = 0;
    while (i<j)
    {
    if (s[i] == s[j])
    {
    b += s[i];
    i++;
    j--;
    }
    else {
    if (j != k)
    {
    j = k;
    }
    else
    {
    i++;
    }
    b.clear();
    }
    }
    if (i == j)
    {
    b += s[i];
    len = 2 * (b.length() - 1) + 1;
    }
    else
    {
    len = 2 * b.length();
    }
    if (len >=cas)
    {
    ans = b;
    cas = len;
    }
    j = k;
    b.clear();
    }
    if (cas%ans.length() == 0)
    {
    for (int i = ans.length() - 1; i >= 0; i--)
    ans += ans[i];
    }
    else
    {
    for (int i = ans.length() - 2; i >= 0; i--)
    ans += ans[i];
    }
    cout <<ans<< endl;
    return 0;
    }
    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.