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"

 

Note: In the string, each word is separated by single space and there will not be any extra space in the string.

public class Solution {
    public String reverseWords(String s) {
        if (s == null)
            return null;
        if (s.length() == 0)
            return "";
        StringBuilder ret = new StringBuilder();
        String[] ss = s.split(" ");
        int cnt = 0;
        for (String st : ss) {
            if (cnt ++ > 0)
                ret.append(" ");
            char[] chs = st.toCharArray();
            for (int i=chs.length-1; i>=0; i--) {
                char ch = chs[i];
                ret.append(ch);
            }
            
        }
        return ret.toString();
    }
}

 

相关文章:

  • 2022-12-23
  • 2022-01-26
  • 2022-02-14
  • 2022-01-03
  • 2021-08-21
  • 2021-12-04
  • 2022-02-11
猜你喜欢
  • 2021-07-29
  • 2022-02-24
  • 2022-12-23
  • 2022-12-23
  • 2022-01-27
  • 2021-05-28
相关资源
相似解决方案