题解 | #翻转单词序列#
翻转单词序列
https://www.nowcoder.com/practice/3194a4f4cf814f63919d0790578d51f3
import java.util.*; public class Solution { public String ReverseSentence(String str) { String ret = ""; int tail = str.length()-1; for (int i = str.length()-1; i >= 0; i--){ if (i == 0 || str.charAt(i) == ' '){ int j = (i == 0) ? 0 : i + 1; while(j <= tail){ ret += str.charAt(j); j++; } if (i != 0){ ret += " "; while(str.charAt(i) == ' '){ i--; } tail = i; if (i == 0){ i++; } } } } return ret; } }