题目
解题思路
解题代码
import java.util.*;
public class Main{
public static void reverse(char[] arr,int start,int end){
while(start < end){
char tmp = arr[start];
arr[start] = arr[end];
arr[end] = tmp;
start++;
end--;
}
}
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
char[] arr = str.toCharArray();
reverse(arr,0,arr.length - 1);
int i = 0;
while(i < arr.length){
int j = i;
while(j < arr.length && arr[j] != ' '){
j++;
}
if(j < arr.length){
reverse(arr,i,j - 1);
i = j + 1;
}else{
reverse(arr,i,j - 1);
i = j;
}
}
String res = new String(arr);
System.out.println(res);
}
}
题链接
倒置字符串链接
|