leetocde557: Reverse Words in a String III

来源:互联网 发布:棋牌游戏数据库 编辑:程序博客网 时间:2024/06/06 09:02

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

Example 1:

Input: "Let's take LeetCode contest"Output: "s'teL ekat edoCteeL tsetnoc"
注意:用StringBuffer 简单的很

public String reverseWords(String s) {if (s.equals(""))return "";String[] sta = s.split(" ");String re = "";for (int i = 0; i < sta.length; i++) {StringBuffer sbf = new StringBuffer(sta[i]);sbf.reverse();sta[i] = sbf.toString();re = re + " " + sta[i];}re = re.substring(1, re.length());return re;}



0 0
原创粉丝点击