Tag: palindrome

缺少Java中的return语句错误

我目前正在用Java编写一个回文测试器,用于我在高中学习的课程。 我已经向老师求助了,他也很困惑。 我希望stackoverflow上的社区可以帮助我。 谢谢。 public class Palindrome { private String sentence; public Palindrome(String s) { sentence = s; } public boolean isPalindrome() { if(sentence.length() <= 1) { return true; } if(sentence.charAt(0) == sentence.charAt(sentence.length()-1)) { sentence = sentence.substring(1, sentence.length()-1); isPalindrome(); } else return false; } }

找到所有作为回文的子串

如果输入是“abba”,则可能的回文是a,b,b,a,bb,abba。 我知道确定字符串是否是回文很容易。 这将是: public static boolean isPalindrome(String str) { int len = str.length(); for(int i=0; i<len/2; i++) { if(str.charAt(i)!=str.charAt(len-i-1) { return false; } return true; } 但找到回文子串的有效方法是什么?

这个Java正则表达式如何检测回文?

这是一系列教育正则表达式文章的第三部分。 它遵循这个正则表达式如何找到三角形数字? (首先介绍嵌套引用)以及如何将^ nb ^ n与Java正则表达式匹配? (前瞻性“计数”机制进一步详述)。 这部分介绍了一种特定forms的嵌套断言,当与嵌套引用结合使用时,Java正则表达式可以匹配大多数人认为“不可能”的东西:回文! 回文的语言是非常规的 ; 它实际上是无上下文的 (对于给定的字母表)。 也就是说,现代正则表达式实现不仅仅识别常规语言,而且Perl / PCRE的递归模式和.NET的平衡组可以很容易地识别回文(参见: 相关问题 )。 但是,Java的正则表达式引擎既不支持这些“高级”function。 然而“某人” ( * wink * )成功编写了以下正则表达式,这似乎做得很好( 参见ideone.com ): public class Palindrome { // asserts that the entirety of the string matches the given pattern static String assertEntirety(String pattern) { return “(?<=(?=^pattern$).*)".replace("pattern", pattern); } public static void main(String[] […]