题目
题解
双指针法,依次交换首尾字符。
public String getReversedOrderString(String s) {
String[] strings = s.split(" ");
for (int i = 0, j = strings.length - 1; i <= j; i++, j--) {
String temp = strings[i];
strings[i] = strings[j];
strings[j] = temp;
}
return Stream.of(strings).collect(Collectors.joining(" "));
}
时间复杂度:O(n)