【问题标题】:How to make this recursive function check if the string is a palindrome?如何使这个递归函数检查字符串是否是回文?
【发布时间】:2020-02-03 05:06:45
【问题描述】:

该函数应该反转字符串,如果前后相同,则返回true。我必须为此使用递归,并且不能为此使用reverse()。我通过调试器运行我的代码,似乎我的代码在检查 s == reverse 时返回 false。

这是我的尝试:

bool Palindrome(const string& s, int i){
string reverse;

    if(i < s.size()){
        reverse[i] = s[s.size()-(i+1)]; // first character in reverse is the last character in s
        Palindrome(s, i + 1);
        }       
    if(s == reverse){
        return true;
    }
    else{
        return false;
    }

}

【问题讨论】:

  • 你输入的字符串是什么?如果您通过调试器运行代码,为什么不单步执行所有操作,看看发生了什么?
  • 我已经很久没有玩过C++了,但我注意到的第一件事是字符串没有初始化,所以你得到了一些可能足够大也可能不够大的默认大小。其次,您不复制空字符,因此字符串可能无法正确终止,因此比较将是错误的。

标签: c++ function recursion reverse palindrome


【解决方案1】:

以下代码显示了您在问题中阐述的示例实现。它使用递归创建一个反向字符串,然后将其与引用字符串进行比较,并返回一个布尔结果。

内存复杂度 O(n)。但实际上,反转字符串确实需要额外的 O(n) 内存。

#include <iostream>
#include <algorithm>
using namespace std;

bool isPalindrome(string& reference, string& reversed, int index){
    if(index >= reference.size()/2){
        return reference == reversed;
    }

    int lastIndex = reference.size()-1;
    swap(reversed[index], reversed[lastIndex-index]);
    return isPalindrome(reference, reversed, index+1);
}

int main() {
    string ref = "abcdcba";
    string test = ref;
    cout << isPalindrome(ref,test,0) << endl;
    return 0;
}

【讨论】:

    【解决方案2】:

    如果您希望Palindrome(const string&amp;) 操作string reverse,您应该通过引用传递它。

    #include <string>
    #include <iostream>
    
    using namespace std;
    
    bool palindrome_internal( const string& s, string& reverse, int i )
    {
        if(i < s.size()){
            reverse[i] = s[s.size()-(i+1)]; // first character in reverse is the last character in s
            palindrome_internal( s , reverse , i + 1);
        }
    
        return s == reverse;
    }
    
    bool Palindrome(const string& s ){
        string reversed { s }; // initialized here
    
        return palindrome_internal( s , reversed , 0 ); // And passed to recursive function
    }
    
    int main()
    {
        cout << Palindrome( "example" ) << endl; // Not palindrome
        cout << Palindrome( "repaper" ) << endl; // Palindrome
        cout << Palindrome( "rotator" ) << endl; // Palindrome
        cout << Palindrome( "madam" ) << endl; // Palindrome
        cout << Palindrome( "" ) << endl; // Palindrome
        cout << Palindrome( "" ) << endl; // Palindrome
    }
    

    online code

    您的代码实际上有点奇怪,因为您没有递归计算回文,实际上您正在递归填充string reverse

    也许这个更简单的版本更适合。

    #include <string>
    #include <iostream>
    
    bool palindrome( const std::string& s, int i = 0 )
    {
        if ( i == s.size() )
            return true;
    
        return s[ i ] == s[ s.size() - i - 1 ] && palindrome( s , i + 1 );
    }
    
    int main()
    {
        using namespace std;
        cout << palindrome( "example" ) << endl; // Not palindrome
        cout << palindrome( "repaper" ) << endl; // Palindrome
        cout << palindrome( "rotator" ) << endl; // Palindrome
        cout << palindrome( "madam" ) << endl; // Palindrome
        cout << palindrome( "" ) << endl; // Palindrome
        cout << palindrome( "a" ) << endl; // Palindrome
    }
    

    online code

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-19
      • 2012-07-14
      • 2013-02-02
      • 1970-01-01
      • 1970-01-01
      • 2013-02-10
      • 1970-01-01
      相关资源
      最近更新 更多