好久沒有參加面試了,今天參加了個電話面試,慘敗呀,以此祭奠一下。
其實題目都不難,但是自己沒有準備,所以答得不好,分享下題目,大家共勉。
1. 介紹下你的大學以及有趣的事情,用英語闡述
2. 介紹項目相關的資訊,各個項目
3. 說下C#裡的抽象方法和虛方法的區別
抽象方法是只有方法簽名,沒有方法體的,且希望由子類來實現
虛方法是有方法體的,由virtual關鍵字標記,子類可以先擇要不要實現他,如果要可以加override關鍵字表示重寫父類的該方法,也可以不加關鍵字或new來覆蓋父類的方法,如果不加關鍵字,編譯器會產生一個警告給予提示。
4. 知道String的contains方法嗎?如果是你你怎麼實現這個方法
當剛被問到這道題時,自己想了一下,不知道怎麼說,可能是因為緊張,也可能是因為沒有去細想,其實這道題不難。
1) 能否使用已有的方法賦值我們實現這個功能,其實這個是有的,String類有個執行個體方法叫indexof(),他有一個重載的版本是對一個string返回所在的index.
public static bool stringContainsUseIndex(String source, string target) { if (source==null || target==null || source == "" || target == "") return false; //invoke the string's instance method Indexof(string value, int startIndex) int result = source.IndexOf(target, 0); if (result >= 0) return true; return false; }
2) 不用系統內建的方法進行實現:
1. 兩個字串,如果被包含的字串(target)長度長於包含的字串(source),那麼這個方法肯定返回false;
2. 要找到與target第一字元匹配的在source裡的位置,並且我們在找的時候沒有必要搜尋所有在source裡的字元,自需要搜尋前source.Lengh-target.Lenth個長度就可以;
3. 如果找到第一個匹配的字元了,記錄當前source裡的index,便開始按target裡的字元來匹配;
4. 如果完全符合了,就返回true, 不是完全符合就返回第3步儲存的index+1處開始重新搜尋第一個匹配字元;
5. 沒有找到完全符合的,最終返回false.
代碼:
public static bool stringContains(String source, string target) { int sourcelengh = source.Length; int targetLengh = target.Length; //compare the lengh, if sourcelengh is smaller than targetlengh, then return false if (sourcelengh < targetLengh) return false; char firstchar = target[0]; int max = sourcelengh - targetLengh; //Mark the max time we would compare the first letter in the worst time int index = -1; while (index <= max) { while (++index <= max && source[index] != firstchar) { } if (index <= max) { //find the first match, and compare the later letters int sourceIndex = index + 1; //we only need to compare targetlengh-1 times to ensure it is contained. int leavelength = sourceIndex + targetLengh - 1; int targetIndex = 1; for (; sourceIndex < leavelength&&source[sourceIndex]==target[targetIndex]; sourceIndex++, targetIndex++) ; if (sourceIndex == leavelength) return true; } } return false; }