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

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

    string result = new StringBuffer(yourString).reverse().toString();
    

    Depending on what you understand under O(1) it does not seem possible since even reading the string once needs O(n) with n being the number of characters in the string.

提交回复
热议问题