用Java设计一个程序,实现一个字符串的对称个数,如字符串"effeghg",有"ff","effe","ghg"这三个对称字符,所以返回3.
我没写出来这个算法.请教高手!!!
我没写出来这个算法.请教高手!!!
解决方案 »
- 如何显示用float类型存储的最大和最小数字?
- Java程序如何控制使只能启动一个程序(就是程序只能单开,不能像QQ那样可以同时挂好几个窗口)
- 如何在桌面应用程序(JFrame框架)中加入网页
- applet在MyEclipse内正常运行,但在html内不能自动更新内容
- 帮帮我这初学者
- Jcreater无法安装
- 刚学JAVA,有点地方不太明白,希望大虾帮忙写来参考下!!!谢谢
- 怎么得到1-100之间的随机整数?
- 我装了jbuilder7,但是不大会用的。请问大家有没有什么教程?
- swing对话框问题?
- ant与junit结合时,想通过扩充ANT,通过读一个外部XML文件把文件中的值读到ANT已有的任务中
- jLabel问题?
/**
*
*/
package com.lim.test.csdn;/**
* @author bzwm
*
*/
public class FindSymmetryStr {
public static int findSymmetryStr(String orgStr) {
int count = 0;
if (orgStr != null && orgStr.length() > 1) {
int size = orgStr.length(); int head; int current;
char hStr;
char cStr; for (head = 0; head < size; head++) {
hStr = orgStr.charAt(head);
current = size - 1;
while (current > head) {
cStr = orgStr.charAt(current); if (hStr == cStr) {
String newStr = orgStr.substring(head + 1, current);
if (newStr.length() > 1)
count += findSymmetryStr(newStr);
else if (newStr.length() == 1)
count++;
else {
count++;
}
current--; } else {
current--;
}
}
}
} return count;
} public static void main(String args[]) {
int count = findSymmetryStr("cddcbcbeffeghg");//
System.out.println("symmetry string count is : " + count);
}
}
http://blog.csdn.net/bzwm/archive/2008/12/22/3577023.aspx希望对你有帮助。
public class Te
{
int sum;
public void check(String s)
{
for(int i=0;i<s.length();i++)
{
char c=s.charAt(i); //取出第一个字符c
String s1=s.substring(i+1); //剩余的字符组成新字符串s1
for(int j=0;j<s1.length();j++)
{
if(c==s1.charAt(j)) //比较c与s1中哪个字符相等
{
if(j==0||j==1) //如果j=0或1,说明相等的两个字符相距0或1(像 ss 和 sws ),此时对称
sum++;
else{ //如果j等于其他,好像swkes,此时就取出wke,重复上述步骤
String s2=s1.substring(0,j);
check(s2);
}
}
}
}
}
public static void main(String []args)
{
Te t=new Te();
t.check("effeghg");
System.out.println(t.sum);
}
}