Java版本的迴文字演算法(java版本)_java

來源:互聯網
上載者:User

廢話不多說了,直接給大家貼代碼了,具體代碼如下所述:

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版本),希望對大家有所協助,如果大家有任何疑問歡迎給我留言,小編會及時回複大家的!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.