Java StringBuilder


  • 0
    D

    """

    public String reverseWords(String s) {
        if(s.length() == 0 || s == null) return s;
        String[] strArr = s.split(" ");
        StringBuilder resBuilder = new StringBuilder();
        for(String word : strArr){
            String revWord = reverse(word);
            resBuilder.append(revWord);
            resBuilder.append(" ");
        }
        String result = resBuilder.toString();
        return result.trim();
    }
    
    private String reverse(String str){
        char[] strArr = str.toCharArray();
        int start = 0, end = str.length() -1;
        while(start < end){
            char temp = strArr[start];
            strArr[start] = strArr[end];
            strArr[end] = temp;
            start+= 1;
            end -=1;
        }
        return String.valueOf(strArr);
    }
    

    """


Log in to reply
 

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