Java solution, StringBuilder, no reverse


  • 0
    J
    public class Solution {
        public String reverseWords(String s) {
            StringBuilder res = new StringBuilder();
            int temp = 0;
            for(int i = 0; i < s.length(); i++){
                if(s.charAt(i) == ' '){
                    res.append(s.charAt(i));
                    temp = i + 1;
                } else {
                    res.insert(temp,s.charAt(i));
                }
            }
            return res.toString();
        }
    }
    
    

    Run time 26ms. How does it compare to the solution using 'reverse' function?


Log in to reply
 

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