回文字算法(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);
}
}
回文字算法(java版本)的更多相关文章
- 通过“回文字算法”复习C++语言。
一.什么是回文字 给定一个字符串,从前往后读和从后往前读,字符串序列不变.例如,河北省农村信用社的客服电话是“96369”,无论从后往前读,还是从前后往后读,各个字符出现的位置不变. 二.功能实现 ( ...
- 微博短链接的生成算法(Java版本)
最近看到微博的短链接真是很火啊,新浪.腾讯.搜狐等微博网站都加入了短链接的功能.之所以要是使用短链接,主要是因为微博只允许发140 字,如果链接地址太长的话,那么发送的字数将大大减少.短链接的主要职责 ...
- POJ 3974 最长回文字串(manacher算法)
题意:给出一个字符串,求出最长回文字串. 思路:一开始我直接上了后缀数组DC3的解法,然后MLE了.看了DISCUSS发现还有一种计算回文字串更加优越的算法,就是manacher算法.就去学习了一下, ...
- 最长回文字串——manacher算法
时间复杂度:O(n) 参考:https://segmentfault.com/a/1190000003914228 1.问题定义 最长回文子串问题:给定一个字符串,求它的最长回文子串长度. 如果一个字 ...
- java实现简单回文算法
算法要求 编写一个程序,判断一个字符串是否为"回文".回文串:字符串字符从前往后与从后往前一致(中心对称). 算法思路 首先将字符串等分左右两块,然后依次对称比较每一对字符是否相同 ...
- 归并排序算法 java 实现
归并排序算法 java 实现 可视化对比十多种排序算法(C#版) [直观学习排序算法] 视觉直观感受若干常用排序算法 算法概念 归并排序是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Di ...
- Atitit 电子商务订单号码算法(java c# php js 微信
Atitit 电子商务订单号码算法(java c# php js 微信 1.1. Js版本的居然钱三爷里面没有..只好自己实现了. 1.2. 订单号标准化...长度16位 1.3. 订单号的结构 前 ...
- 求字符串的最长回文字串 O(n)
昨天参加了某公司的校园招聘的笔试题,做得惨不忍睹,其中就有这么一道算法设计题:求一个字符串的最长回文字串.我在ACM校队选拔赛上遇到过这道题,当时用的后缀数组AC的,但是模板忘了没写出代码来. 回头我 ...
- JGibbLDA:java版本的LDA(Latent Dirichlet Allocation)实现、修改及使用
转载自:http://blog.csdn.net/memray/article/details/16810763 一.概述 JGibbLDA是一个java版本的LDA(Latent Dirichl ...
随机推荐
- atitit 点播系统 概览 v2 qb1.docx
atitit 点播系统 概览 v2 qb1.docx 1.1. 多界面(可以挂载多个不同的界面主题)1 1.2. 独立的选片模块(跨设备,跨平台)2 1.3. 跨设备平台(android安卓盒子,pc ...
- Bootstrap的学习
Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架. Bootstrap 是基于 HTML.CSS.JAVASCRIPT <link href="http://c ...
- MVC利用URLRoute实现伪静态
routes.MapRoute( "Default", // Route name "{con ...
- InstallShield Limited Edition for Visual Studio 2013 图文教程(教你如何打包.NET程序)
InstallShield Limited Edition for Visual Studio 2013 图文教程(教你如何打包.NET程序) 标签: InstallShieldVS2013 2015 ...
- 前端工程师技能之photoshop巧用系列扩展篇——自动切图
× 目录 [1]初始设置 [2]自动切图 前面的话 随着photoshop版本的不断升级,软件本身增加了很多新的功能,也为切图工作增加了很多的便利.photoshop最新的版本新增了自动切图功能,本文 ...
- maven中使用junit老是找不到包
如题,烦恼好久,突然看到scope一直是test,改成compile就好了. compile (编译范围) compile是默认的范围:如果没有提供一个范围,那该依赖的范围就是编译范围.编译范围依赖在 ...
- angularJS配合bootstrap动态加载弹出提示内容
1.bootstrp的弹出提示 bootstrap已经帮我们封装了非常好用的弹出提示Popover. http://v3.bootcss.com/javascript/#popovers 2.自定义p ...
- jquery实现轮播图
/** * Created by hui on 2015/11/3. */ $(function(){ var circleLi = $('.poster-item'); var liLength = ...
- Ajax长轮询
前台代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Wait.asp ...
- 【Remoting】.Net remoting方法实现简单的在线升级(下篇:重启exe)
一.前言 上篇运用了.Net Remoting技术解决了本地与服务器版本对比,并下载更新包的过程. 本篇主要是应用Process,来实现重启程序的过程. 情景假设: Revit2 ...