public class Solution {
public static String reverserWords(String s){
String[] Array=s.split(" ");
int len=Array.length;//length of array
int low=0;int high=len-1;int i=0;
while(i<len && low <=high){
String temp=null;
temp=Array[i];
Array[i]=Array[len-1-i];
Array[len-1-i]=temp;
low++;high--;i++;
}//for
// for(int k=0;k<len;k++)
// System.out.println(Array[k]);
for(int j=1;j<len;j++)
Array[0]+=" "+Array[j];
return Array[0];
}
public static void main(String[] args){
String test="the sky is blue";
String newtest=reverserWords(test);
System.out.println(newtest);//the output should be blue is sky the
}
}
leetcode题目:reverse words in a string
最新推荐文章于 2021-05-22 06:35:20 发布