1 line java,use Stream,but not quick


  • 0
    T
    public class Solution {
        public String reverseWords(String s) {
            return Stream.of(s.split(" ")).map(t->" "+new StringBuffer(t).reverse().toString()).reduce("", String::concat).substring(1);
        }
    }
    

  • 0
    Q

    @TQQQ
    You can use String.join method to replace your reduce and concat function:

    public class Solution {
        public String reverseWords(String s) {
            return String.join(" ", Arrays.stream(s.split(" ")).map(str -> new StringBuilder(str).reverse().toString()).collect(Collectors.toList()));
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.