废话不多说了 , 直接给大家贴代码了 具体代码如下所述 :
paquete com.gdh.backText; import java.util.hashmap; import java.util.map; import java.util.map.entry; public class backtext {string text; public backtext () {super (); this.text = null;} public BackText (texto de cadena) {super (); this.text = text;} public boolean isBackText () {for (int i = 0, j = text.length ()-i-1; i <= j; i ++, j-) {if (text.charat (i)! = text.charat (j)) {return false; }} return true;} public map <caracteres, integer> countString () {map <caracteres, integer> map = new HashMap <> (); int count = 0; Cadena temp = new String (); for (int i = 0; i <text.length (); i ++) {if (temp.indexof (text.charat (i), 0) <0) {temp+= text.charat (i); }} map.clear (); for (int i = 0; i <temp.length (); i ++) {if (! map.containskey (temp.charat (i))) {for (int j = 0; j <text.length (); j ++) {if (text.charat (j) == temp.charat (i)) {Count ++; }} map.put (temp.charat (i), contar); recuento = 0; }} // 循环打印 para (Entrada <carácter, entero> item: map.Entryset ()) {System.out.println ("字符:" + item.getKey () + "值:" + item.getValue ()); } mapa de retorno; } public String Convert () {int chechSum = 0; int itemCount = 0; Map <caracteres, integer> map = countString (); para (Entrada <carácter, entero> elemento: map.entryset ()) {checksum+= item.getValue (); if (item.getValue () %2! = 0) itemCount ++; } if (itemCount> 1) {System.out.println ("该字符串不能转换为回文字"); regresar nulo; } StringBuffer temp = new StringBuffer (text); // 线程安全 // StringBuilder temp = new StringBuilder (); // 线程非安全 int begidx = 0; int endidx = checksum-1; Clave de personaje = nulo; bandera booleana = falso; para (Entrada <caracteres, entero> elemento: map.Entryset ()) {if (checkSum % 2 == 0) {for (int i = 0; i <item.getValue ()/2; i ++) {temp.setcharat (Begidx ++, item.getkey ()); temp.setcharat (endidx--, item.getKey ()); }} else {if (item.getValue ()%2 == 0) {for (int i = 0; i <item.getValue ()/2; i ++) {temp.setcharat (begidx ++, item.getkey ()); temp.setcharat (endidx--, item.getKey ()); }} else {key = item.getKey (); bandera = verdadero; continuar; }}} if (flag) {for (int i = 0; i <map.get (key); i ++) {temp.setcharat (begidx ++, key); }} return temp.ToString ();} public static void main (string [] args) {backText bt = new BackText ("1122334455667788990"); if (! bt.isbackText ()) system.out.println ("该字符串不是回文字"); else System.out.println ("该字符串是回文字"); String Dest = new String (); System.out.println ("开始转换 ..."); des = bt.convert (); System.out.print ("转换后的结果为:"); System.out.println (Dest); }}以上所述是小编给大家介绍的 java 版本的回文字算法 (java 版本) , 希望对大家有所帮助 , 如果大家有任何疑问欢迎给我留言 小编会及时回复大家的! 小编会及时回复大家的!