From 2e9d51194e1a4d8dfc4e1529ffe29187e3fac0ce Mon Sep 17 00:00:00 2001 From: harryzhuang Date: Tue, 23 May 2023 10:11:58 +0800 Subject: [PATCH] update --- src/TwoPointer/ReverseWords.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/TwoPointer/ReverseWords.java diff --git a/src/TwoPointer/ReverseWords.java b/src/TwoPointer/ReverseWords.java new file mode 100644 index 0000000..eec85e7 --- /dev/null +++ b/src/TwoPointer/ReverseWords.java @@ -0,0 +1,21 @@ +package TwoPointer; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; + +public class ReverseWords { + + + public static String reverse(String input) { + String[] splittedStrs = input.trim().split(" +"); + Collections.reverse(Arrays.asList(splittedStrs)); + return String.join(" ", splittedStrs); + } + + public static void main(String[] a){ + System.out.println("input1 res:" + reverse("the sky is blue")); + System.out.println("input2 res:" + reverse(" hello world ")); + + } +}