什么是最有效的算法为扭转在Java中的字符串? [英] What is the most efficient algorithm for reversing a String in Java?

查看:140
本文介绍了什么是最有效的算法为扭转在Java中的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是扭转一个字符串在Java中最有效的方法是什么?我应该使用某种形式的XOR运算符的?最简单的方法是把所有的字符以栈,并把它们放回一个字符串再次,但我怀疑这是一个非常有效的方法来做到这一点。

What is the most efficient way to reverse a string in Java? Should I use some sort of xor operator? The easy way would be to put all the chars in a stack and put them back into a string again but I doubt that's a very efficient way to do it.

和请不要告诉我使用一些内置的功能在Java中。我有兴趣学习如何做到这一点不使用一个有效的功能,但不知道为什么它的效率或它是如何建立起来的。

And please do not tell me to use some built in function in Java. I am interested in learning how to do it not to use an efficient function but not knowing why it's efficient or how it's built up.

推荐答案

你说你想知道最有效的方式,你不想知道的一些标准,这样做的内置方式。然后,我对你说:RTSL(读取源,卢克):

You say you want to know the most efficient way and you don't want to know some standard built-in way of doing this. Then I say to you: RTSL (read the source, luke):

查看源$ C ​​$下 AbstractStringBuilder #reverse ,这被称为StringBuilder的#相反。我敢打赌,它的一些东西,你就不会认为是一个强大的反向操作。

Check out the source code for AbstractStringBuilder#reverse, which gets called by StringBuilder#reverse. I bet it does some stuff that you would not have considered for a robust reverse operation.

这篇关于什么是最有效的算法为扭转在Java中的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆