我花了好几个小时试图在这种背景下找到回文的逻辑。所以我们给出了一个单词的字符串的arraylist,我们需要从单词列表中找到最大的回文。例如,["mr", "owl", "ate", "my", "metal", "worm", "racecar", "mad", "am"]
将使用以下结果["mrowlatemymetalworm", "racecar", "madam"]
构建一个araylist。到目前为止,我一直在玩迭代,但似乎无法获得如何从两端迭代的正确逻辑,特别是当它从另一端切换内部字符串索引时...这就是我的意思到目前为止。
List<String> list = Arrays.asList("mr", "owl", "ate", "my", "metal", "worm", "racecar", "mad", "am");
List<String> palindromeList = new ArrayList<String>();
int i = 0;
int j = list.get(i).length();
int l = list.size()-1;
int s = list.get(l).length() -1;
while (i<j){
while (j<list.get(i).length()){
if (s == 0){
//need to reinitialize s for previous index of back end of list for possible palindrome
}
if (list.get(l).charAt(s) == list.get(i).charAt(j)){
l--;
}
else if (list.get(l).charAt(s) != list.get(i).charAt(j)){
j++;
s--;
}
}
}
//once outer loop breaks the result should be added to palindromeList
答案 0 :(得分:1)
你可以通过比较它是否与自身相反来确认字符串是否为回文(这是确切的定义):
public static boolean isPalindrome(String value) {
if (value == null || value.isEmpty())
return false;
return new StringBuilder(value).reverse().toString().equals(value);
}
我不确定我是否理解你想要应用的逻辑,但根据你给出的输入和输出,我想出了类似的东西:
List<String> list = Arrays.asList("mr", "owl", "ate", "my", "metal", "worm", "racecar", "mad", "am");
List<String> palindromeList = new ArrayList<String>();
for (int i = 0; i < list.size(); ++i) {
String longestPalindrome = null;
String candidate = "";
for (int j = i; j < list.size(); ++j) {
candidate += list.get(j);
if (isPalindrome(candidate))
longestPalindrome = candidate;
}
if (longestPalindrome != null)
palindromeList.add(longestPalindrome);
}
答案 1 :(得分:0)
为了检测一个字符串是否是回文,我将字符串分成两半,反转字符串,看看两边是否相等。
有两种情况:
Word包含多个字符:
如果字符串是奇数长度,我们将字符串拆分为2个子字符串,不包括中间字符:例如:ABCBA将分为AB和BA。然后我们将字符串反转并进行比较以查看它们是否相等。
Word的字符数为奇数:
如果字符串是偶数长度,我们只需将字符串拆分为2个相等大小的子字符串并反转其中一个,然后进行比较以查看它们是否是相同的字符串。例如:汉娜将是汉和不。
List<String> list = Arrays.asList("mr", "owl", "ate", "my", "metal", "worm", "racecar", "mad", "am");
List<String> palindromeList = new ArrayList<String>();
//Detects if a string is a palindrome.
for (int i = 0; i < list.size(); i ++) {List<String> palindromeList = new ArrayList<String>();
int wordLength = list.get(i);
String leftSide = "";
String rightSide ="";
if (wordLength%2 == 1) { //If word has odd number of characters.
leftSide = list.get(i).subString(0,wordLength/2);
rightSide = list.get(i).subString((wordLength/2) + 1, wordLength);
} else { //If word has even number of characters.
leftSide = list.get(i).subString(0,(wordLength/2));
rightSide = list.get(i).subString((wordLength/2), wordLength);
}
String reversedLeftSide = new StringBuilder(leftSide).reverse().toString();
if (reversedLeftSide.equals(rightSide)) {
palindromeList.add(list.get(i));
}
}
String longestPalindrome = "";
//Searches for longest palindrome in the list of palindromes.
for (int i = 0; i < palindromeList.size(); i++) {
if (palindromeList.get(i).length() > longestPalindrome.length()) {
longestPalindrome = palindromeList.get(i);
}
}
System.out.println(longestPalindrome); //This should give you longest palindrome.
请记住,可能有十几种方法可以解决这个问题,我刚刚提出了一个解决方案。此代码可能还包含1或2个小错误,因为它尚未经过测试。
答案 2 :(得分:0)
了解单词是否可以重复是很重要的。如果他们可以,那么你可以拥有&#34; racecarracecar&#34;,&#34; racecarracecarracecar&#34;等,所以我认为他们不能。在这种情况下,我们可以如下进行。浏览单词列表,从&#34; mr&#34;开始。如果回文开始于&#34; mr&#34;它必须以&#34; m&#34;结束。是吗&#34; am&#34;工作?不。尝试其他可能性; &#34;蜗杆&#34;作品。然后是&#34; o&#34;必须遵循&#34; mr&#34;。开头的下一个词必须是&#34; owl&#34;。这意味着最后的下一个词必须以&#34; l&#34;所以必须是&#34; metal&#34;。
等等,直到你1)用完了文字,2)有一个回文,或3)有一个类似于&#34; doggod&#34;单词边界正好在中间,在这种情况下,你可以(递归地)用剩余的单词搜索更多的回文并将你得到的任何东西放在中间,例如&#34; dogracecargod&#34;或者&#34; dogmadamgod&#34;或者&#34; dogmrowlatemymetalwormgod&#34;。