服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java版本的回文字算法(java版本)

Java版本的回文字算法(java版本)

2020-06-20 11:41高殿华 Java教程

本文给大家分享一段java代码关于回文字算法的实例代码,代码简单易懂,需要的朋友一起看看吧

废话不多说了,直接给大家贴代码了,具体代码如下所述:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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<Character,Integer> countString(){
  Map<Character,Integer> 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<Character,Integer> item:map.entrySet()){
    System.out.println("字符:" + item.getKey() + " 值:" + item.getValue());
  }
  return map;
}
public String convert(){
  int checksum = 0;
  int itemcount=0;
  Map<Character,Integer> map=countString();
  for(Entry<Character,Integer> 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<Character,Integer> item: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();
        flag=true;
        continue;
      }
    }
  }
  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("开始转换...");
    dest=bt.convert( ) ;
    System.out.print("转换后的结果为:");
    System.out.println(dest);
  }
}

以上所述是小编给大家介绍的Java版本的回文字算法(java版本),希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

原文链接:http://www.cnblogs.com/gaodianhua/archive/2016/10/01/5925488.html

延伸 · 阅读

精彩推荐