2个问题1.有一个邮箱,有一千万条黑名单用户,怎么判断收件箱是不是黑名单用户。据说和字典什么的是属于同一个经典问题。。2.求一个字符串中最长的颠倒字符串。。比如 a123ghfuhg321asd131.(就是a123,321a.)
6 回答

Cats萌萌
TA贡献1805条经验 获得超9个赞
public class Test {
/** * @param args */ public static void main(String[] args) { String a = "jha123ghfs343uhg321asd131"; String str1 = null, str2 = null, tmpstr1 = null, tmpstr2 = null, maxstr1 = null, maxstr2 = null; for (int i = 0; i < a.length() - 2; i++) { for (int j = 2 + i; j < a.length() - 1; j++) { if (j > i) { str1 = a.substring(i, j); if (str1 != null && !str1.equals("") && str1.length() > 1) { str2 = (new StringBuilder(str1)).reverse().toString(); if (a.indexOf(str2) == -1) { if (tmpstr1 != null && (maxstr1 == null || tmpstr1.length() > maxstr1.length())) { maxstr1 = tmpstr1; maxstr2 = tmpstr2; } break; } else { tmpstr1 = str1; tmpstr2 = str2; } } } } } System.out.println("字符串中最长的颠倒字符串:"+maxstr1 + "," + maxstr2); }
}
运行输出,字符串中最长的颠倒字符串:a123gh,hg321a
添加回答
举报
0/150
提交
取消