题目:
思路:
按空格拆分
反转拆分后数组中的每一个串(双指针)
用StringBuffer 进行连接
(一)代码
class Solution { public String reverseWords(String s) { String[] strs = s.split(" "); StringBuffer sb = new StringBuffer(); for(int i = 0 ; i < strs.length ; i++){ char[] chars = strs[i].toCharArray(); int begin = 0; int end = chars.length-1; while(begin <= end){ swap(chars,begin++,end--); } sb.append(chars); if(i != strs.length - 1){ sb.append(" "); } } return sb.toString(); } public void swap(char[] chars,int begin,int end){ char temp = chars[begin]; chars[begin] = chars[end]; chars[end] = temp; } }
减减肥