下午好,我正在this页面上进行练习,我失败了两个测试用例:
小写单词:错误答案
任何案例单词:错误答案
我无法理解,他们的意思是我,因为在算法中,我控制大小写。
我的算法如下:
using System;
public class Palindrome
{
public static char[] abecedario = new char[]{ 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','w','x','y','z'};
public static char[] ABECEDARIO = new char[]{ 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','W','X','Y','Z'};
public static bool IsPalindrome(string str)
{
string strOriginal = string.Empty;
string strInversa = string.Empty;
for(int i = str.Length-1; i >= 0; i--)
{
string concatenarInversa = string.Empty;
string concatenarOriginal = string.Empty;
for(int j = 0; j < abecedario.Length; j++)
{
if(str[i] == abecedario[j] || str[i] == ABECEDARIO[j])
{
concatenarInversa = abecedario[j].ToString();
}
if(str[(str.Length-1)-i] == abecedario[j] || str[(str.Length-1)-i] == ABECEDARIO[j] )
{
concatenarOriginal = abecedario[j].ToString();
}
if(concatenarInversa!=string.Empty && concatenarOriginal != string.Empty)
{
break;
}
}
if(concatenarInversa!=string.Empty)
{
strInversa = strInversa + concatenarInversa;
}
if(concatenarOriginal != string.Empty)
{
strOriginal = strOriginal + concatenarOriginal;
}
}
Console.WriteLine("strInversa : " + strInversa);
Console.WriteLine("strOriginal : " + strOriginal);
return strInversa == strOriginal;
}
public static void Main(string[] args)
{
Console.WriteLine(IsPalindrome("sEeS"));
}
}
答案 0 :(得分:0)
您的abecedario
个变量缺少字母V(v)。试试这个:
public static char[] abecedario = new char[]{ 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
public static char[] ABECEDARIO = new char[]{ 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
或者(更容易打字):
public static char[] abecedario = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
public static char[] ABECEDARIO = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
对于测试的示例,您当前的代码失败,请考虑单词“vee”。它不是回文,但因为你的代码忽略了字母V,它认为它是。