废话不多说了,直接给大家贴代码了,具体代码如下所述:
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 countString(){
Map 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();
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), count);
count=0;
}
}
//循环打印
for(Entry item:map.entrySet()){
System.out.println("字符:" + item.getKey() + " 值:" + item.getValue());
}
return map;
}
public String convert(){
int checksum = 0;
int itemcount=0;
Map map=countString();
for(Entry item:map.entrySet()){
checksum+=item.getValue();
if( item.getValue() %2 != 0)
itemcount++;
}
if( itemcount > 1 ){
System.out.println("该字符串不能转换为回文字");
return null;
}
StringBuffer temp=new StringBuffer(text);//线程安全
//StringBuilder temp=new StringBuilder();//线程非安全
int begIdx=0;
int endIdx=checksum-1;
Character key=null;
boolean flag=false;
for(Entry item:map.entrySet()){
if( checksum % 2 ==0 ){
for(int i=0;i
以上所述是小编给大家介绍的Java版本的回文字算法(java版本),希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!



