Whats the best way to recursively reverse a string in Java?

后端 未结 26 2885
一个人的身影
一个人的身影 2020-11-27 14:56

I have been messing around with recursion today. Often a programming technique that is not used enough.

I set out to recursively reverse a string. Here\'s what I cam

26条回答
  •  星月不相逢
    2020-11-27 15:38

    String rev="";
    
    public String reverseString(String s){
            if (s.length()==0) return "";
            return rev+s.substring(s.length()-1,s.length())+reverseString(s.substring(0, s.length()-1));
        }
    

提交回复
热议问题