创建回文的递归方法
我正在尝试在 Java 中使用递归创建一个回文程序,但我陷入困境,这就是我到目前为止所拥有的:
public static void main (String[] args){
System.out.println(isPalindrome("noon"));
System.out.println(isPalindrome("Madam I'm Adam"));
System.out.println(isPalindrome("A man, a plan, a canal, Panama"));
System.out.println(isPalindrome("A Toyota"));
System.out.println(isPalindrome("Not a Palindrome"));
System.out.println(isPalindrome("asdfghfdsa"));
}
public static boolean isPalindrome(String in){
if(in.equals(" ") || in.length() == 1 ) return true;
in= in.toUpperCase();
if(Character.isLetter(in.charAt(0))
}
public static boolean isPalindromeHelper(String in){
if(in.equals("") || in.length()==1){
return true;
}
}
}
任何人都可以为我的问题提供解决方案吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
这是三个简单的实现,首先是 oneliner:
这当然很慢,因为它创建了一个字符串缓冲区并将其反转,并且无论它是否是回文,总是检查整个字符串,所以这里是一个仅检查所需的实现字符数量并就地执行,因此没有额外的 stringBuffers:
并且递归地:
Here are three simple implementations, first the oneliner:
This is ofcourse quite slow since it creates a stringbuffer and reverses it, and the whole string is always checked nomatter if it is a palindrome or not, so here is an implementation that only checks the required amount of chars and does it in place, so no extra stringBuffers:
And recursively:
试试这个:
Try this:
这是一个将忽略指定字符的递归方法:
像这样使用它:
在递归方法中包含不区分大小写是没有意义的,因为它只需要执行一次,除非不允许使用
。 toLowerCase() 方法。
Here is a recursive method that will ignore specified characters:
Use it like this:
It does not make sense to include case insensitivity in the recursive method since it only needs to be done once, unless you are not allowed to use the
.toLowerCase()
method.没有比这更小的代码了:
如果你想检查一些东西:
LOL B-]
there's no code smaller than this:
if you want to check something:
LOL B-]
该解决方案不区分大小写。因此,例如,如果您有以下单词:“adinida”,那么如果您执行“Adninida”或“adninida”或“adinidA”,您将得到 true,这就是我们想要的。
我喜欢@JigarJoshi 的答案,但他的方法的唯一问题是,对于包含大写字母的单词,它会给你错误的结果。
This solution is not case sensitive. Hence, for example, if you have the following word : "adinida", then you will get true if you do "Adninida" or "adninida" or "adinidA", which is what we want.
I like @JigarJoshi answer, but the only problem with his approach is that it will give you false for words which contains caps.
回文示例:
调用者:
如果是回文则打印 true,否则打印 false。
如果字符串的长度为 0 或 1(不再需要检查字符串),则返回 true,作为基本情况。在此之前的函数调用将引用此基本情况。然后通过每次递归调用函数时截掉第一个和最后一个字母来比较第一个和最后一个字母是否相等。
Palindrome example:
Called by:
Will print true if palindrome, will print false if not.
If the length of the string is 0 or 1(no more string to check), return true, as the base case. This base case will be referred to by function call right before this. Then compare to see if the first and last letters are equal, by cutting off the first and last letters each time the function is recursively called.
这是在不创建许多字符串的情况下进行回文检查的代码
Here is the code for palindrome check without creating many strings
公共类 PlaindromeNumbers {
}
public class PlaindromeNumbers {
}
简单的解决方案
2 场景 --(奇数或偶数长度字符串)
基本条件&算法递归(ch, i, j)
i==j //偶数len
if i<; j 递归调用 (ch, i +1,j-1)
else return ch[i] ==ch[j]// 旧长度的额外基本条件
Simple Solution
2 Scenario --(Odd or Even length String)
Base condition& Algo recursive(ch, i, j)
i==j //even len
if i< j recurve call (ch, i +1,j-1)
else return ch[i] ==ch[j]// Extra base condition for old length
为了实现这一点,您不仅需要知道递归的工作原理,还需要了解 String 方法。
这是我用来实现它的示例代码:-
for you to achieve that, you not only need to know how recursion works but you also need to understand the String method.
here is a sample code that I used to achieve it: -
我在这里为您粘贴代码:
但是,我强烈建议您了解它是如何工作的,
从您的问题来看,您完全无法阅读。
尝试理解这段代码。 阅读代码中的注释
Here I am pasting code for you:
But, I would strongly suggest you to know how it works,
from your question , you are totally unreadable.
Try understanding this code. Read the comments from code
嗯:
我暂时不会把它说得更清楚,因为我怀疑这是家庭作业 - 事实上,有些人可能认为上面的帮助太多了(我自己当然有点犹豫)。如果您对上述提示有任何疑问,请更新您的问题以显示您已经完成了多少。
Well:
I'm not going to spell it out any more clearly than that for the moment, because I suspect this is homework - indeed some may consider the help above as too much (I'm certainly slightly hesitant myself). If you have any problems with the above hints, update your question to show how far you've got.
也许你需要这样的东西。未经测试,我不确定字符串索引,但它是一个起点。
Maybe you need something like this. Not tested, I'm not sure about string indexes, but it's a start point.
我认为,递归不是解决这个问题的最佳方法,但我在这里看到的一种递归方法如下所示:
I think, recursion isn't the best way to solve this problem, but one recursive way I see here is shown below:
这是我的做法:
Here is my go at it:
有些代码是字符串重的。我们可以在递归调用中传递索引,而不是创建创建新对象的子字符串,如下所示:
Some of the codes are string heavy. Instead of creating substring which creates new object, we can just pass on indexes in recursive calls like below: