Skip to content

Latest commit

 

History

History
48 lines (39 loc) · 1.16 KB

557. Reverse Words in a String III.md

File metadata and controls

48 lines (39 loc) · 1.16 KB

leetcode-cn Daily Challenge on August 30th, 2020.


Difficulty : Easy

Related Topics : String


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.


Solution

  • mine
    • Java
      public String reverseWords(String s) {
          StringBuffer buffer = new StringBuffer();
          String[] splits = s.split(" ");
          for(int i = 0; i < splits.length; i++){
              String temp = splits[i];
              for(int j = temp.length() - 1; j >= 0; j--){
                  buffer.append(temp.charAt(j));
              }
              if(i + 1 != splits.length){
                  buffer.append(" ");   
              }
          }
          
          return buffer.toString();
      }