Clean and easy java solution without split() or reverse().


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

Log in to reply
 

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