废话不多说了, 直接给大家贴代码了, 具体代码如下所述:
แพ็คเกจ com.gdh.backtext; นำเข้า java.util.hashmap; นำเข้า java.util.map; นำเข้า java.util.map.entry; คลาสสาธารณะ backtext {ข้อความสตริง; backtext สาธารณะ () {super (); this.text = null;} backtext สาธารณะ (ข้อความสตริง) {super (); this.text = text;} public boolean isbacktext () {สำหรับ (int i = 0, j = text.length ()-i-1; i <= j; i ++, j-) {ถ้า (text.charat (i)! = text.charat (j)) {return false; }} return true;} แผนที่สาธารณะ <อักขระ, จำนวนเต็ม> countString () {แผนที่ <อักขระ, จำนวนเต็ม> แผนที่ = ใหม่ hashmap <> (); จำนวน int = 0; สตริงอุณหภูมิ = สตริงใหม่ (); สำหรับ (int i = 0; i <text.length (); i ++) {ถ้า (temp.indexof (text.charat (i), 0) <0) {temp+= text.charat (i); }} map.clear (); สำหรับ (int i = 0; i <temp.length (); i ++) {ถ้า (! map.containskey (temp.charat (i))) {สำหรับ (int j = 0; j <text.length (); j ++) {ถ้า (text.charat (j) == temp.charat (i)) }} map.put (temp.charat (i) นับ); นับ = 0; }} // 循环打印สำหรับ (รายการ <อักขระ, จำนวนเต็ม> รายการ: map.entryset ()) {system.out.println ("字符:" + item.getKey () + "值:" + item.getValue ()); } กลับแผนที่; } การแปลงสตริงสาธารณะ () {int checksum = 0; int itemCount = 0; แผนที่ <อักขระ, จำนวนเต็ม> map = countString (); สำหรับ (รายการ <อักขระ, จำนวนเต็ม> รายการ: map.entryset ()) {checksum+= item.getValue (); if (item.getValue () %2! = 0) itemCount ++; } if (itemCount> 1) {system.out.println ("该字符串不能转换为回文字"); คืนค่า null; } StringBuffer temp = new StringBuffer (text); // 线程安全 // StringBuilder temp = new StringBuilder (); // 线程非安全 int begidx = 0; int endidx = checksum-1; คีย์อักขระ = null; ธงบูลีน = เท็จ; สำหรับ (รายการ <อักขระ, จำนวนเต็ม> รายการ: map.entryset ()) {ถ้า (checksum % 2 == 0) {สำหรับ (int i = 0; i <item.getValue ()/2; i ++) {temp.setcharat (Begidx ++, item.getKey (); Temp.Setcharat (endidx--, item.getKey ()); }} else {ถ้า (item.getValue ()%2 == 0) {สำหรับ (int i = 0; i <item.getValue ()/2; i ++) {temp.setcharat (begidx ++, item.getKey ()); Temp.Setcharat (endidx--, item.getKey ()); }} else {key = item.getKey (); ธง = จริง; ดำเนินการต่อ; }}} if (Flag) {สำหรับ (int i = 0; i <map.get (key); i ++) {temp.setcharat (begidx ++, key); }} ส่งคืน temp.toString ();} โมฆะคงที่สาธารณะหลัก (สตริง [] args) {backtext bt = backtext ใหม่ ("1122334455667788990"); if (! bt.isbacktext ()) system.out.println ("该字符串不是回文字"); อื่น System.out.println ("该字符串是回文字"); สตริง dest = new String (); System.out.println ("开始转换 ... "); dest = bt.convert (); System.out.print ("转换后的结果为:"); System.out.println (dest); -以上所述是小编给大家介绍的 Java 版本的回文字算法 (Java 版本), 希望对大家有所帮助, 如果大家有任何疑问欢迎给我留言, 小编会及时回复大家的! 小编会及时回复大家的!