剑指offer32 JZ73 翻转单词序列
翻转单词序列
https://www.nowcoder.com/practice/3194a4f4cf814f63919d0790578d51f3?tpId=13&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D1%26tpId%3D13%26type%3D13
栈
思路: 入栈后在出栈 顺序就会发生变化
import java.util.*;
public class Solution {
public String ReverseSentence(String str) {
Stack<String> stack=new Stack<>();
String[] temp=str.split(" ");
//入栈
for(int i=0;i<temp.length;i++){
stack.push(temp[i]);
stack.push(" ");
}
StringBuilder res = new StringBuilder();
if(!stack.isEmpty()){
//取出顶部空格
stack.pop();
}
while(!stack.isEmpty()){
res.append(stack.pop());
}
return res.toString();
}
}