应该使用什么逻辑来获取该模式中的输出?在pattern中,每个数字表示字符串中给定单词的位置。
Example 1:
i/p: A dead fox found in the forest
o/p: tserof ni eht dnuof xof A daed
pattern 7 5 6 4 3 1 2
Example 2:
i/p: A dead fox found in the deep forest
o/p: tserof eht peed ni dnuof daed xof A
8 6 7 5 4 2 3 1发布于 2021-10-27 06:19:36
这将是一个反转字符串的函数。
public static String reverseString(String str)
{
return new StringBuilder(str).reverse().toString();
}要执行关于您的模式的另一部分:
String[] arrayWithStrings = new String[]{"A", "dead", "fox", "found", "in", "the", "forest"};
int[] pattern = new int[]{7, 5, 6, 4, 3, 1, 2};
for (int i : pattern)
{
System.out.println(reverseString(arrayWithStrings[i - 1]));
}这是相当微不足道的,因为模式显示了单词在句子中的位置。如果您必须解析整个句子,而不是array[]。然后,您可以获得字符串形式的句子,并使用split(" ")从其中获取一个数组。
尽管如此,仅仅在这里解决问题是非常令人沮丧的。自己做点什么,试一试!
https://stackoverflow.com/questions/69733648
复制相似问题