2個問題1.有一個郵箱,有一千萬條黑名單用戶,怎么判斷收件箱是不是黑名單用戶。據(jù)說和字典什么的是屬于同一個經(jīng)典問題。。2.求一個字符串中最長的顛倒字符串。。比如 a123ghfuhg321asd131.(就是a123,321a.)
6 回答

達(dá)令說
TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超6個贊
第一個問題,用hash,把字符串轉(zhuǎn)為數(shù)字去比對,數(shù)據(jù)的存儲和查找都會比較快。
第二個問題,回文問題,上網(wǎng)找吧!

Cats萌萌
TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超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); }
}
運(yùn)行輸出,字符串中最長的顛倒字符串:a123gh,hg321a
添加回答
舉報(bào)
0/150
提交
取消