如何找到给定字符串中最长的回文?
可能的重复:
编写一个返回最长回文的函数在给定的字符串中
我知道如何在 O(n^2) 内做到这一点。但似乎存在更好的解决方案。
我发现 this,并且有一个指向 O(n) 答案的链接,但它是用 Haskell 编写的,我不清楚。
如果能得到 c# 或类似的答案那就太好了。
Possible Duplicate:
Write a function that returns the longest palindrome in a given string
I know how to do this in O(n^2). But it seems like there exist a better solution.
I've found this, and there is a link to O(n) answer, but it's written in Haskell and not clear for me.
It would be great to get an answer in c# or similar.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我找到了解决方案的明确解释 此处。感谢贾斯汀提供此链接。
在那里您可以找到该算法的 Python 和 Java 实现(C++ 实现包含错误)。
这里是 C# 实现,它只是这些算法的翻译。
I've found clear explanation of the solution here. Thanks to Justin for this link.
There you can find Python and Java implementations of the algorithm (C++ implementation contains errors).
And here is C# implementation that is just a translation of those algorithms.
这是它的 java 版本:
And this is its java version:
C#
首先,我搜索偶数长度的回文。然后我搜索奇数长度的回文。当它找到回文时,它会确定长度并相应地设置最大长度。平均情况复杂度是线性的。
C#
First I search for even length palindromes. Then I search for odd length palindromes. When it finds a palindrome, it determines the length and sets the max length accordingly. The average case complexity for this is linear.
最近我在面试时写了以下代码......
Recently I wrote following code during interview...
我在接受采访的时候就被问到了这个问题。
不幸的是,当我回到家时我才发现。
I got this question when i took an interview.
I found out when i was back home, unfortunately.