My AC solutoins beats 90% Java


  • 0
    E
    public class Solution {
        public String reverseWords(String s) {
         char [] arr = s.toCharArray();
            int cwl = 0;
         for(int k=0;k<arr.length;k++){
             if(arr[k] == ' ' && cwl>0){
                 int begin = k -cwl;
                 int end = begin + cwl -1;
                 helper(arr,begin,end,cwl);
                 cwl=0;
             }else{
                 cwl++;
             }
            
         }
         
         if(cwl>0){
            int begin = arr.length -cwl;
             int end = begin + cwl -1;
            helper(arr,begin,end,cwl);
         }
         return new String(arr);
        }
        void helper(char[] arr,int begin,int end,int cwl){
            while(begin<end){
                     char temp  = arr[begin];
                     arr[begin] = arr[end];
                     arr[end] = temp;
                     begin++;
                     end--;
            }
        }
    }
    

Log in to reply
 

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