题解 | #翻转单词序列#
翻转单词序列
https://www.nowcoder.com/practice/3194a4f4cf814f63919d0790578d51f3
import java.util.*; public class Solution { public String ReverseSentence(String str) { String[] str_list = str.split(" "); /****funtion 1 Stack<String> str_stack = new Stack<>(); for (int i =0; i < str_list.length; ++i) { str_stack.push(str_list[i]); } String res = str_stack.pop(); while (!str_stack.isEmpty()) { res += " " + str_stack.pop(); } **/ int len = str_list.length; String res = str_list[len-1]; for (int i = len-2; i >=0; --i) { res += " " + str_list[i]; } return res; } }
讲真的, 这个用java写, 要比用c++合适很多;
毕竟java里面是有对string的追加和split的操作