废话不多说了 , 直接给大家贴代码了 , 具体代码如下所述:
package 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 (String text) {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 <caractères, entier> countstring () {map <caractère, entier> map = new hashmap <> (); int count = 0; String 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 (); pour (int i = 0; i <temp.length (); i ++) {if (! map.containsKey (temp.Carat (i))) {for (int j = 0; j <text.length (); j ++) {if (text.charat (j) == temp.charat (i)) {count ++; }} map.put (temp.charat (i), count); count = 0; }} // 循环打印 pour (entrée <caractère, entier> élément: map.entryset ()) {System.out.println ("字符:" + item.getKey () + "值:" + item.getValue ()); } retour de la carte; } public String convert () {int Checksum = 0; int itemCount = 0; Map <caractères, entier> map = countstring (); pour (entrée <caractère, entier> item: map.entrySet ()) {Checksum + = item.getValue (); if (item.getValue ()% 2! = 0) itemCount ++; } if (itemCount> 1) {System.out.println ("该字符串不能转换为回文字"); retourner null; } StringBuffer temp = new StringBuffer (texte); // 线程安全 // stringBuilder temp = new StringBuilder (); // 线程非安全 int begidx = 0; int EndIdx = Checksum-1; Key de caractère = null; booléen drapeau = false; pour (entrée <caractère, entier> item: map.entrySet ()) {if (Checksum% 2 == 0) {for (int i = 0; i <item.getValue () / 2; i ++) {temp.secharat (BegIdx ++, item.getKey ()); Temp.Setcharat (ENDIDX--, item.getKey ()); }} else {if (item.getValue ()% 2 == 0) {for (int i = 0; i <item.getValue () / 2; i ++) {temp.secharat (begidx ++, item.getKey ()); Temp.Setcharat (ENDIDX--, item.getKey ()); }} else {key = item.getKey (); Flag = true; continuer; }}} if (Flag) {for (int i = 0; i <map.get (key); i ++) {temp.secharat (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 ("开始转换 ..."); dest = bt.convert (); System.out.print ("转换后的结果为:"); System.out.println (dest); }}以上所述是小编给大家介绍的 Java 版本的回文字算法 (java 版本) , 希望对大家有所帮助 , 如果大家有任何疑问欢迎给我留言 , 小编会及时回复大家的!