翻转字符串里的单词 字符串处理

 

给定一个字符串,逐个翻转字符串中的每个单词。

示例 1:

输入: "the sky is blue"
输出: "blue is sky the"
 leetcode https://leetcode-cn.com/problems/reverse-words-in-a-string

//数据结构学太狠,满脑子satck
//待改进
class Solution { public String reverseWords(String s) { Stack stack = new Stack<String>(); String[] strs = s.split("\s+"); if(strs.length==0) return ""; int count = 0; for(int i=0;i<strs.length;i++) { if(strs[i].equals(" ")||strs[i]==null) break; stack.push(strs[i]); System.out.print("LYY"+strs[i]); count++; } String result = ""; for(int i=0;i<count;i++) { result+=" "; result+=stack.pop(); } return result.trim(); } }
原文地址:https://www.cnblogs.com/luiyuying/p/12661321.html