7.25 - medium總結(jié)21

388. Longest Absolute File Path:很巧妙的答案,把"\t"做為當(dāng)前深度,然后計(jì)算當(dāng)前深度的前綴長(zhǎng)度=上一個(gè)深度+文件名長(zhǎng)度+1

class Solution(object):
    def lengthLongestPath(self, input):
        """
        :type input: str
        :rtype: int
        """
        maxlen = 0
        pathlen = {0: 0}
        for line in input.split("\n"):
            name = line.lstrip('\t')
            depth = len(line) - len(name)
            if '.' in name:
                maxlen = max(maxlen, pathlen[depth] + len(name))
            else:
                pathlen[depth + 1] = pathlen[depth] + len(name) + 1
        return maxlen

390. Elimination Game: 這種題目,直接暴力求解的話TLE了,不過這樣的題目,除了多做幾遍,把答案背下來,真不知道怎么去理解:https://leetcode.com/problems/elimination-game/#/discuss 基本的想法是維護(hù)左邊的head的值,從左朝右的時(shí)候,head = head + step 從右朝左的時(shí)候,如果l % 2 == 1也就是奇數(shù),也就是能刪掉當(dāng)前的head,那么就是head = head + step。
392. Is Subsequence: 難一點(diǎn)的在于followup問題,可以用binary search 和trie來做
393. UTF-8 Validation:這題基本思路是會(huì)的,只是寫法有點(diǎn)問題,太繁瑣,再重寫一遍
394. Decode String: 雖然磕磕絆絆的,也算是用stack做出來了吧
395. Longest Substring with At Least K Repeating Characters: 想了半天的前向型雙指針,后來發(fā)現(xiàn)不對(duì)。只要簡(jiǎn)單的遞歸就可以了
396. Rotate Function: 又是一道沒想出來的,感覺思路接近了,就是沒想出來
397. Integer Replacement: 又是一題不會(huì)做,今天狀態(tài)又變差了嗎?

class Solution(object):
    def integerReplacement(self, n):
        """
        :type n: int
        :rtype: int
        """
        rtn = 0
        while n > 1:
            rtn += 1
            if n % 2 == 0:
                n //= 2
            elif n % 4 == 1 or n == 3:
                n -= 1
            else:
                n += 1
        return rtn

398. Random Pick Index: 又一道神奇的Reservoir Sampling。
399. Evaluate Division:創(chuàng)建一個(gè)帶權(quán)重的有向圖,但是狀態(tài)不好,今天不想做了,先標(biāo)記下來,明天再做吧。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容