[Interview ]1. Arrays and Strings

Hash Tables

ArrayList & Resizeable Arrays

amortized insertion running time is O(1)

StringBuilder

The time complexity for using "+" to concatenate a string array is O(x + 2x + 3x + ... + nx) = O(xn^2).

String joinWords(String[] words) {
      String sentence = "";
      for (String w : words) {
            sentence = sentence + w;
      }
      return sentence;
}

StringBuidler helps avoid the problem by using a resizable array of all the strings.

Exercise Method

Impelement your own version of StringBuilder, HashTable and ArrayList.

Additional Reading

  • Hash Table Collision Resolution (pg 636)
  • Rabin-Karp Substring Search (pg636)

Interview Question

1.1 Is Unique

Implement an algorithm to determine if a string has all unique characters. What if you cannot use an additional data structure?
Idea: use a hash set to put all seen chars, check each char with the hash set to see if it has been seen before. -- This uses an additional data structure, a hash set.

Solution

  1. first check if the string is an ASCII string or a Unicode string.
  2. 如果字符串長度超過字母表中所有字母的個數(shù),則一定存在重復字符。
    方法一:
    使用boolean數(shù)組表示每個字符是否出現(xiàn)。例如如果字符串是ASCII則可以初始化一個長度為256的字符數(shù)組表示每個字符是否出現(xiàn)。-> 時間復雜度O(n), 空間復雜度O(1)

方法二:
使用bit vector表示一個字符是否出現(xiàn),每一位表示一個字符。

如果不能使用額外的數(shù)據(jù)結(jié)構(gòu):

  1. 使用每個字符以此與其他字符進行比較 -> 時間復雜度O(n^2)
  2. 如果可以修改字符串,則可以先排序,再比較相鄰字符,時間復雜度O(n * logn)

1.2 Check Permutation

Given two strings, write a method to decide if one is a permutation of the other
**Idea

  1. 如果兩個字符串具有完全相同的字母組合則認為這兩個字符串互為排列,因此,可以計算每個字符串里面字符出現(xiàn)的個數(shù)進行比較,字符串計數(shù)的方法通過一個int數(shù)組決定
  2. 可以先對兩個字符串進行排列,排列后逐個字母進行對比

Solution
需要預(yù)先了解大小寫空格是否對結(jié)果有影響

1.3 URLify

URLify: Write a method to replace all spaces in a string with '%20'. You may assume that the string has sufficient space at the end to hold the additional characters, and that you are given the "true" length of the string. (Note: if implementing in Java, please use a character array so that you can perform this operation in place.)
EXAMPLE
Input: "Mr John Smith ", 13
Output: "Mr%20John%20Smith"

Idea
首先統(tǒng)計字符串中有多少個空格,由此推算將空格替換為%20后字符串有多長。如果原來字符串長度為len,其中有c個空格,那么處理后的字符串長度為len + 2c;因此我們可以得到擴展之后最后一個字符應(yīng)該出現(xiàn)的位置。
對字符進行urlify的過程將從字符串的結(jié)尾向前進行。

1.4 Palindrome Permutation -> todo 實現(xiàn)優(yōu)化算法的代碼

EXAMPLE
Input: Tact Coa
Output: True (permutations: "taco cat'; "atco eta·; etc.)

Idea
對字符串中的數(shù)組進行計數(shù),如果最多只有一個字符出現(xiàn)的次數(shù)為計數(shù)則認為這個字符串可以通過排列得到一個回文字符串。忽略其中的空白字符串

Solution
優(yōu)化:使用bit vector表示一個字符出現(xiàn)次數(shù)的奇偶性

1.5 One Away -> todo 實現(xiàn)代碼

There are three types of edits that can be performed on strings: insert a character, remove a character, or replace a character. Given two strings, write a function to check if they are one edit (or zero edits) away.
EXAMPLE
pale, ple -> true
pales, pale -> true
pale, bale -> true
pale, bae -> false

Idea:
因為判斷只需要修改以此,那么首先判斷兩個字符串的長度差別是不是1,如果大于1則肯定不符合條件。
逐個判斷兩個字符串是否相等,如果不相等,則可以通過以下方式進行修改 => 首先假設(shè)字符串為A和B,當前比較的index為 i 和 j

  1. 如果A(i) 不等于 B(j),但是A(i) = B(j+1)則可以通過向A中填充B(j)實現(xiàn)修改,i不變,j=j+1繼續(xù)比較
  2. 如果A(i+1) = B(j+1),則可以通過修改A(i) = B(j)達到目的
  3. 如果A(i+1) = B(j)則可以刪除A(i),那么i = i +1 繼續(xù)比較

在出現(xiàn)一次edit后將一個flag變量設(shè)置為true,如果再次需要修改則返回false

Solution
長度差異決定需要修改的方式!!!

1.6 String Compression -> 實現(xiàn)代碼

Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2blc5a3. If the "compressed" string would not become smaller than the original string, your method should return the original string. You can assume the string has only uppercase and lowercase letters (a - z).

Idea
注意:如果壓縮之后字符串的長度不小于原字符串的長度則放棄壓縮。

  1. 首先判斷一個壓縮后的字符串長度與原字符串長度的大小
  2. 如果需要則壓縮字符串,使用一個int數(shù)組記錄每個字符出現(xiàn)的個數(shù)

1.7 Rotate Matrix -> todo 實現(xiàn)代碼

Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?

1.8 Zero Matrix -> todo 實現(xiàn)代碼

Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0.

Idea
使用第一行和第一列作為標志位,并使用isColZero和isRowZero兩個標志表示第一列和第一行本身是否為0.
首先遍歷整個數(shù)組,如果一個元素為0,則將它所在的第一行和第一列對應(yīng)的位置設(shè)置為0。
再次遍歷數(shù)組,根據(jù)第一行和第一列的標志信息將對應(yīng)的行和列設(shè)置為0

1.9 String Rotation

Assume you have a method isSubstring which checks if one word is a substring of another. Given two strings, 51 and 52, write code to check if 52 is a rotation of 51 using only one call to isSubstring (e.g., "waterbottle" is a rotation of" erbottlewat").

解法厲害!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,443評論 6 532
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,530評論 3 416
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,407評論 0 375
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,981評論 1 312
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,759評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,204評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,263評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,415評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,955評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 40,782評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,983評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,528評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,222評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,650評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,892評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,675評論 3 392
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,967評論 2 374