題目:輸入兩個字符串,從第一字符串中刪除第二個字符串中所有的字符。例如,
輸入”They are students.”和”aeiou”,則刪除之後的第一個字符串變成”Thy r
stdnts.”。
其實這類題有個特點,字符串中的字符分爲兩類,就可以聯想快速排序裏的將當前的數組分爲左右兩組,其中左邊的數字小於某值,右邊的數字大於某值。這種大於和小於就是將分爲兩類。當然,等於也是,但是可以忽略。
還有一道面試題,將int數組轉換爲奇數偶數各一邊。
這道題就是將字符分爲在刪除字符串中 和不在刪除字符串中兩種。
public static void delete(String source, String dest){
boolean[] del = new boolean[128];
Arrays.fill(del, false);
char[] src = new char[source.length()];
src = source.toCharArray();
int j = 0;
for(int i=0; i<dest.length(); i++){
del[dest.charAt(i)] = true;
}
for(int i=0; i<source.length(); i++){
char c = source.charAt(i);
if(!del[c]){
src[j] = c;
j++;
}
}
System.out.println(new String(src,0, j));
}