C++编码逻辑 - 各种想法
我想编写一个方法来确定给定的字符串是否是回文。例如“女士,我是亚当”,或“一个男人,一个计划,一条运河,巴拿马”。
该函数的原型是:
bool is_palindrome(char const * str)
我有一个简单的逻辑,通过前进和后退来检查是否相等。从弦的最末端向后。但是,我想知道有多少种有效的方法可以做到这一点?欢迎来自 C++ 专家的所有想法..
I want to write a method to determine if a given string is a palindrome. E.g. "Madam I'm Adam", or "A man, a plan, a canal, Panama".
The prototype for the function is:
bool is_palindrome(char const * str)
I have a simple logic to check for equality by moving forward & backward from extreme ends of the string. But, i would like to know how many efficient ways to do this ? All ideas welcome from C++ gurus..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为没有更有效的方法,您必须比较字符串中的每个字符。
可能的优化:只需检查字符串的前半部分,一旦发现不匹配就可以尽早突破。
I don't think there is a much more efficient way, you do have to compare every character in the string.
Possible optimisations: You only have to check the first half of the string, and you can break out early as soon as you find a mismatch.