剑指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();
    }
}
全部评论

相关推荐

群星之怒:不是哥们,你就不好奇瘫痪三十年的老植物人是啥样的吗?
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务