My java code


  • 0
    S
    public class Solution {
        public String reverseWords(String s) {
            String []strArr = (s.trim()).split(" +");
            int firstWord = 0; int lastWord = strArr.length-1;
            
            while(firstWord < lastWord) {
                swapWords(strArr, firstWord, lastWord);
                firstWord++; lastWord--;
            }
            return String.join(" ", strArr);
        }
        public void swapWords(String[] strArr, int i, int j) {
            String temp = strArr[i];
            strArr[i] = strArr[j];
            strArr[j] = temp;
        }
    }
    

Log in to reply
 

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