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

后端 未结 10 1994
被撕碎了的回忆
被撕碎了的回忆 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:29

    Here I have a sample of the same using substring method and o(n). I am aware that using substring will hold complete string memory.

    for(int i = 0; i < s.length(); i++)    {
        s = s.substring(1, s.length() - i) + s.charAt(0) + s.substring(s.length() - i);
        System.out.println(s);
    }
    

    This might help you. Tell me if I am wrong!!

提交回复
热议问题