【发布时间】:2016-01-10 23:13:15
【问题描述】:
我一直在寻找有效的方法来检查字符串是否旋转回文,除了 O(n^2) 解决方案之外,我发现 SO post 谈到了看起来相当复杂的 O(n) 解决方案。我写了下面的代码,我认为它也以更简单的哈希表格式实现了 O(n) 的解决方案。有人可以看看它,看看我是否遗漏了什么或者我的解决方案是正确的?
/*Code doesnt cover scenarios where character is caps and space*/
int main(void)
{
char str[] = "123217898";
char *pstr = str;
int length = strlen(str);
int notdouble = 0;
int arr[256] = { 0 };
for (int i = 0; i < length; i++)
{
arr[*pstr]++;
pstr++;
}
pstr = str;
for (int i = 0; i < length; i++)
{
/*check if its divisible by 2 and greater than 2*/
if (arr[*pstr] % 2 != 0 && arr[*pstr] > 2)
{
notdouble = 5; // just assign random value greater than 1
}
if (arr[*pstr] == 1)
{
notdouble++;
}
pstr++;
}
if (notdouble > 1)
{
printf("string is not palindrome \n");
}
else
{
printf("string is palindrome \n");
}
return 0;
}
【问题讨论】:
-
我投票结束这个问题,因为它是关于代码审查的(试试codereview.stackexchange.com)。
-
以前问过question
-
@WeatherVane - 我本人已经提供了该链接 - 问题讨论了更简单的替代解决方案。我正在寻找答案来检查我的逻辑是否存在漏洞,因为没有人建议这样做
-
哦 - 对不起。当我在谷歌上搜索“旋转回文”时,它是最高条目
-
@OliverCharlesworth - 不仅仅是代码审查,我正在寻找对部分逻辑的验证,它提供了比相关的复杂方法更简单的替代方案。如果人们不同意这里,我不介意将其发布在代码审查中..