Zillow面試題匯總(2)

遞歸計算字符串長

def length(self, s):
    if s == '\n':
        return 0
    else:
        return 1 + self.length(s[1:])

Given a 2d array of numbers, write a function that all matching pairs of numbers. Return a structure that contains the value and positions of each pair. Use each location in the array only once.

Example input:
1 3 4 1
6 1 7 8
9 0 5 8
2 2 1 2
Example Return:
[{value:1, x1:0, y1:0, x2:1, y2:1},
{value:1, x1:2, y1:3, x2:3, y2:0},
{value:8, x1:3, y1: 2, x2:3, y2:1},
{value:2, x1:0, y1:3, x2:1, y2:3}]

Product of Array Except Self (LC 238)

Solve it without division and in O(n).
For example, given [1,2,3,4], return [24,12,8,6]

class Solution:
    def productExceptSelf(self, nums):
        p = 1
        n = len(nums)
        output = []
        for i in range(0,n):
            output.append(p)
            p = p * nums[i]
        p = 1
        for i in range(n-1,-1,-1):
            output[i] = output[i] * p
            p = p * nums[i]
        return output

Majority Element (LC169)

return sorted(num)[len(num)/2]

Lowest Common Ancestor of a Binary Tree (LC236)

def lowestCommonAncestor(self, root, p, q):
    if root in (None, p, q): return root
    left, right = (self.lowestCommonAncestor(kid, p, q)
                   for kid in (root.left, root.right))
    return root if left and right else left or right

Valid Parentheses (LC 20)

class Solution(object):
    def isValid(self, s):
        rp = [')', ']', '}']
        d = {')': '(', ']': '[', '}': '{'}
        l = []
        if s[0] in rp:
            return False
        for p in s:
            if p in rp and len(l) > 0:
                if l.pop() == d[p]:
                    continue
                else:
                    return False
            else:
                l.append(p)
        return l == []

shuffle a string so that no two ajacent characters are same

Similar to LC 358

Writing your own square root function

(http://stackoverflow.com/questions/1623375/writing-your-own-square-root-function)

Find all subsets of a set. (LC 78)

# Iteratively
def subsets(self, nums):
    res = [[]]
    for num in sorted(nums):
        res += [item+[num] for item in res]
    return res

Swap nodes in pairs in a linked list. (LC 24)

def swapPairs(self, head):
    pre, pre.next = self, head
    while pre.next and pre.next.next:
        a = pre.next
        b = a.next
        pre.next, b.next, a.next = b, a, b.next
        pre = a
    return self.next

Decision Tree construction

(i.e., how to compute entropy and information gaim)

reverse integer

def reverse(self, x):
    s = cmp(x, 0)
    r = int(`s*x`[::-1])
    return s*r * (r < 2**31)

給一個string input誊册, eg:“appleE”诈茧, 統(tǒng)計每個character的個數(shù)域仇,然后按照character的字母先后順序撮奏,打印letter和次數(shù)贮聂, eg: output of “appleE” is “E1a1e1l1p2”. 這里大小寫是區(qū)分的, 所以更容易些狡相。

string. ascii_letters/string. ascii_lowercase/string.ascii_uppercase

Group Anagrams

LC 49

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末呀癣,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子男娄,更是在濱河造成了極大的恐慌行贪,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,496評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件模闲,死亡現(xiàn)場離奇詭異建瘫,居然都是意外死亡,警方通過查閱死者的電腦和手機尸折,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評論 3 392
  • 文/潘曉璐 我一進店門啰脚,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人实夹,你說我怎么就攤上這事橄浓。” “怎么了亮航?”我有些...
    開封第一講書人閱讀 162,632評論 0 353
  • 文/不壞的土叔 我叫張陵荸实,是天一觀的道長。 經(jīng)常有香客問我缴淋,道長准给,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,180評論 1 292
  • 正文 為了忘掉前任重抖,我火速辦了婚禮露氮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘钟沛。我一直安慰自己畔规,他們只是感情好,可當我...
    茶點故事閱讀 67,198評論 6 388
  • 文/花漫 我一把揭開白布恨统。 她就那樣靜靜地躺著叁扫,像睡著了一般三妈。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上陌兑,一...
    開封第一講書人閱讀 51,165評論 1 299
  • 那天沈跨,我揣著相機與錄音,去河邊找鬼兔综。 笑死,一個胖子當著我的面吹牛狞玛,可吹牛的內(nèi)容都是我干的软驰。 我是一名探鬼主播,決...
    沈念sama閱讀 40,052評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼心肪,長吁一口氣:“原來是場噩夢啊……” “哼锭亏!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起硬鞍,我...
    開封第一講書人閱讀 38,910評論 0 274
  • 序言:老撾萬榮一對情侶失蹤慧瘤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后固该,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體锅减,經(jīng)...
    沈念sama閱讀 45,324評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,542評論 2 332
  • 正文 我和宋清朗相戀三年伐坏,在試婚紗的時候發(fā)現(xiàn)自己被綠了怔匣。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,711評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡桦沉,死狀恐怖每瞒,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情纯露,我是刑警寧澤剿骨,帶...
    沈念sama閱讀 35,424評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站埠褪,受9級特大地震影響浓利,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜组橄,卻給世界環(huán)境...
    茶點故事閱讀 41,017評論 3 326
  • 文/蒙蒙 一荞膘、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧玉工,春花似錦羽资、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽推汽。三九已至规脸,卻和暖如春腰鬼,著一層夾襖步出監(jiān)牢的瞬間劣领,已是汗流浹背浓恳。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評論 1 269
  • 我被黑心中介騙來泰國打工琢感, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留脏答,地道東北人糕殉。 一個月前我還...
    沈念sama閱讀 47,722評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像殖告,于是被迫代替她去往敵國和親阿蝶。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,611評論 2 353

推薦閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗黄绩。 張土汪:刷leetcod...
    土汪閱讀 12,743評論 0 33
  • 來源: http://www.douban.com/group/topic/14820131/ 調(diào)整變量格式: f...
    MC1229閱讀 6,917評論 0 5
  • (轉(zhuǎn)自http://www.douban.com/group/topic/14820131/羡洁,轉(zhuǎn)自人大論壇) 調(diào)整...
    f382b3d9bdb3閱讀 10,441評論 0 8
  • 昨夜又有失眠,回想去過去的美好爽丹,可卻又不免惋惜筑煮。因為這些美好都是逝去不復(fù)返的。關(guān)于逝去的粤蝎,我有兩個遺憾真仲,是對自己自...
    SoniaZhang閱讀 193評論 0 0
  • 剛下班袒餐,快到家了,初高中的老同學(xué)谤狡,問她買好了票回家嗎灸眼?她回:"沒,搭我哥哥車回去墓懂。" 一來二往聊了幾句焰宣,突然他問:...
    毛毛靜丫閱讀 241評論 0 0