Reverse a string in Java, in O(1)?

后端 未结 10 1995
被撕碎了的回忆
被撕碎了的回忆 2020-12-31 01:47

Is there any facility in the standard Java libraries that, given a CharSequence, produces the reverse in O(1) time?

I guess this is \"easy\" to implement, just wond

10条回答
  •  再見小時候
    2020-12-31 02:17

    // worked hard to figure this out

    public static String ReverseString(String s){
        if (s.length()>0){
            s=s.charAt(s.length()-1)+printString(s.substring(0,s.length()-1));
        }
        return s;
    }
    

提交回复
热议问题