回文字算法(java版本)

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);
  }
}

时间: 2024-08-24 03:42:20

回文字算法(java版本)的相关文章

通过“回文字算法”复习C++语言。

一.什么是回文字 给定一个字符串,从前往后读和从后往前读,字符串序列不变.例如,河北省农村信用社的客服电话是“96369”,无论从后往前读,还是从前后往后读,各个字符出现的位置不变. 二.功能实现 (一).给定一个字符串,判断该字符串是否是回文字. (二).给定一个任意字符串,判断是否可以转换为回文字,如果可以转换为回文字,给出具体的算法. 三.C++语言实现版本(JAVA语言版本后续实现) (一)头文件 (BackText.h) /* * BackText.h * * Created on:

微博短链接的生成算法(Java版本)

最近看到微博的短链接真是很火啊,新浪.腾讯.搜狐等微博网站都加入了短链接的功能.之所以要是使用短链接,主要是因为微博只允许发140 字,如果链接地址太长的话,那么发送的字数将大大减少.短链接的主要职责就是把原始链接很长的地址压缩成只有6 个字母的短链接地址,当我们点击这6 个字母的链接后,我们又可以跳转到原始链接地址. 开始以为短链接是按照某种算法把原始链接压缩为短链接,再根据算法从短链接反算成原始链接的.后来尝试了下压缩算法(gzip 压缩算法),发现对于url 这种字符串越是压缩,长度就越长

【LeetCode-面试算法经典-Java实现】【125-Valid Palindrome(回文字验证)】

[125-Valid Palindrome(回文字验证)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. &quo

Hdu 3068 最长回文字串Manacher算法

题目链接 最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7976    Accepted Submission(s): 2735 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.回文就是正反读都是一样的字符串,如aba, abba等 Input 输入

查找附近网点geohash算法及实现 (Java版本)

参考文档: http://blog.csdn.net/wangxiafghj/article/details/9014363geohash  算法原理及实现方式 http://blog.charlee.li/geohash-intro/  geohash:用字符串实现附近地点搜索 http://blog.sina.com.cn/s/blog_7c05385f0101eofb.html    查找附近点--Geohash方案讨论 http://www.wubiao.info/372        

2238&quot;回文字串&quot;报告

题目描述: 回文串,就是从前往后和从后往前看都是一样的字符串.那么现在给你一个字符串,请你找出该字符串中,长度最大的一个回文子串. 输入描述: 有且仅有一个仅包含小写字母的字符串,保证其长度不超过5000 输出描述: 有且仅有一个正整数,表示最长回文子串的长度 输入样例: abccbxyz 输出样例: 4 分析: 我的算法算是比较暴力的.这道题目中,需要输出最长的回文字串的长度,我的想法是从最长的长度开始寻找是否有回文字串符合这个长度,如果没有,则从次长的长度寻找是否有符合长度的回文字串.比如说

Atitit 电子商务订单号码算法(java c# php js 微信

Atitit 电子商务订单号码算法(java c# php js  微信 1.1. Js版本的居然钱三爷里面没有..只好自己实现了. 1.2. 订单号标准化...长度16位 1.3. 订单号的结构 前4是自定义的,商户可以根据商户业务的不同,头四位不同.例如ET业务为4000,邮件业务为5000 1.4. 统一的订单结构,前边以为可以表示那个接口的订单(可以运用与微信,支付宝,银联表示 订单格式可用字符串,中间下划线分割,方便识别.纯数字兼容性更好 Atitit 微信支付总结 v2 paf 微信

git 回滚到上个版本命令以及忽略某些文件提交

1.git回滚到上个版本 git reset --hard FETCH_HEAD 2.git忽略某些文件的提交 以前是用默认的.gitignore 然后再里面默认某些文件不提交.但是有个问题,.gitignore忽略不掉.因为这是一个全局的.可以改为这样子: 找到 隐藏的.git文件夹,在里面编辑.git/info/exclude文件(如果没有info则创建,默认是没有的),把要忽略的给编辑到exclude里面. 语法与.gitignore一致.两个文件区别在于,exclude只用于设置本地需要

微信开发资料大全 java版本;教程文档、代码、视频

一.官网 1.1 账号申请 https://mp.weixin.qq.com 1.2 开发者模式 1.1 登录后,选择开发者中心 1.3 测试账号申请 http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login 1.4 开发接口文档 http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html 1.5 调试工具接口 http://mp.weixin.qq.