Java Solution without using reverse()


  • 0
    W
    class Solution {
        public String reverseWords(String s) {
            int bar = -1;
            StringBuilder sb = new StringBuilder();
            for(int i=0;i<s.length();i++){
                if(s.charAt(i)!=' '&&bar==-1){
                    bar = i;
                    sb.insert(bar,s.charAt(i));
                    continue;
                }
                if(s.charAt(i)!=' ')
                    sb.insert(bar,s.charAt(i));
                if(s.charAt(i)==' '){
                    bar = -1;
                    sb.append(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.