【问题标题】:Stuck Finding Bug in LeetCode Question Solution在 LeetCode 问题解决方案中卡住了寻找错误
【发布时间】:2023-03-21 09:21:01
【问题描述】:

问题很简单:取一个字符串,然后只反转字母(小写或大写)的位置。将任何特殊字符留在原处。我的解决方案:

char * reverseOnlyLetters(char * S){
    
    int Len = strlen(S);
    char *StrBeg, *StrEnd, tempCh;
    bool FoundStart = 0, FoundEnd = 0;
    
    StrBeg = S;
    StrEnd = S + (Len - 1);
    
    for (int i = 0; i < (Len/2); i++)
    {
        if (((*StrBeg>= 'A') && (*StrBeg <= 'Z')) || ((*StrBeg >= 'a') && (*StrBeg <= 'z')))            
        {
            FoundStart = 1;
        }
        else
        {
            StrBeg++;
        }
        
        if (((*StrEnd >= 'A') && (*StrEnd <= 'Z')) || ((*StrEnd >= 'a') && (*StrEnd <= 'z')))            
        {
            FoundEnd = 1;
        }
        else
        {
            StrEnd--;
        }
        
        if(FoundStart && FoundEnd)
        {
            tempCh = *StrEnd;
            *StrEnd = *StrBeg;
            *StrBeg = tempCh;
            
            StrBeg++;
            StrEnd--;
            
            FoundStart = 0;
            FoundEnd = 0;
        }
            
    }

    return S;
}

问题是像“a-bC-dEf-ghIj”这样的测试用例失败;中间的“E”和“f”要么根本不交换,要么(我怀疑)交换,然后交换回来。有人看到我做错了吗?

【问题讨论】:

  • 这是您通常使用调试器逐行执行代码、检查变量值并最终在逻辑中找到错误的地方。跨度>
  • 花絮:使用 &lt;ctype.h&gt; 标头中的 isalpha() 宏;调用 isalpha(*StrBeg) 进行相同的测试,但更快更干净。
  • 您的循环执行Len/2 次。在每次迭代中,它会根据情况增加/减少 StrBeg 或 StrEnd 或两者。虽然它在前面或后面找到了一个字母,并且正在递增/递减另一个指针以在其中找到一个字母,但在前面/后面没有取得任何进展。因此,可能需要更多的Len/2 迭代才能完全通过一侧。您的循环需要不同的终止条件。
  • 也许是for (int i = 0; i &lt; (Len/2); i++) --> while (StrBeg &lt;= StrEnd)? (也许检测到没有进展)
  • @chux - 恢复 Monica 使用了您对 while 循环的建议。完美运行。谢谢。

标签: c reverse c-strings letter function-definition


【解决方案1】:

问题是 (int i = 0; i

【讨论】:

    【解决方案2】:

    使用这个for循环的方法

    for (int i = 0; i < (Len/2); i++)
    

    不正确。假设字符串是"@AB"。结果字符串看起来像"@BA"

    但是使用你的循环你会得到(因为Len / 2 等于1)

    for (int i = 0; i < 1; i++)
    

    在循环的第一次和单次迭代中,指针StrBeg 将递增

    StrBeg++;
    

    因为指向的字符不是字母。

    所以一切都不会逆转。

    函数可以更简单的写成下面的方式

    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    char * reverse_letters( char *s )
    {
        for ( char *first = s, *last = s + strlen( s ); first < last; ++first )
        {
            while ( first != last && !isalpha( ( unsigned char )*first ) ) ++first;
            while ( last != first && !isalpha( ( unsigned char )*--last ) );
            
            if ( first != last )
            {
                char c = *first;
                *first = *last;
                *last = c;
            }
        }
        
        return s;
    }
    
    int main( void ) 
    {
        char s[] = "one, two, three";
        puts( s );
        puts( reverse_letters( s ) );
    
        char s1[] = "@AB";
        puts( s1 );
        puts( reverse_letters( s1 ) );
    }   
    

    程序输出是

    one, two, three
    eer, hto, wteno
    @AB
    @BA
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-24
      • 2021-05-05
      • 1970-01-01
      • 1970-01-01
      • 2022-11-15
      • 1970-01-01
      • 1970-01-01
      • 2019-01-16
      相关资源
      最近更新 更多