题目:颠倒一个句子中的词的顺序,比如将“我叫克丽丝”转换为“克丽丝叫我”,实现速度最快,移动最少
题目有误,假设是英文句子吧
public static String reverseWord(String word){
char[] ch = word.toCharArray();
char temp;
for (int i = 0; i < ch.length / 2; i++) {
temp = ch[i];
ch[i] = ch[ch.length - i - 1];
ch[ch.length - i - 1] = temp;
}
return String.valueOf(ch);
}
public static String reverse(String words){
String newWords = reverseWord(words);
System.out.println(newWords);
String[] wordSplit = newWords.split(" ");
for(int i = 0;i<wordSplit.length;i++){
wordSplit[i] = reverseWord(wordSplit[i]);
}
return String.join(" ",wordSplit);
}
网友评论