Simple Java Code


  • 0
    F
    public class Solution {
        public String reverseWords(String s) {
            StringBuilder sb = new StringBuilder();
            String[] str = s.split(" ");
            for(String sub: str){
                 sb.append(reverse(sub)).append(" ");
            }
            return sb.deleteCharAt(sb.length()-1).toString();
        }
        
        public String reverse(String in){
            StringBuilder sb = new StringBuilder();
            for(int i = in.length()-1;i>=0;i--){
                sb.append(in.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.