68-72 这片题好难

68  文本左右对齐  这困难题是我自己做的 耶~!

!我觉得亮点在加空格那里

class Solution:
    def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
        n = len(words)
        i = 1 
        rel = []
        temp = []           #用来存储每一行的结果
        lentemp = 0             #用来记录这一行加入多少词了
        temp.append(words[0])
        lentemp += len(words[0])      先把第一个单词加入
        while i < n:
            if lentemp + len(words[i]) < maxWidth:        #后面单词加入长度不超 就先加个空格 再加个单词 注意小于号
                temp.append(' ')
                lentemp += 1
                temp.append(words[i])
                lentemp += len(words[i])
            else:                         #加入这个单词长度超了  就该算这一行的账了 
                spacelen = maxWidth - lentemp         #算算要加多少空格
                if len(temp) == 1:               #如果只有一个单词  就后面添空格
                    temp.append(' '*spacelen)
                    spacelen = 0
                while spacelen > 0:      #不是只有一个单词  temp的奇数位都是空格  从左往右让这些空格变长  。
                    for j in range(1,len(temp),2):      
                        temp[j] +=' '
                        spacelen -= 1
                        if spacelen == 0:          #空格添够了
                            break
                cur = ''                   #处理这一行的temp  
                for each in temp:
                    cur += each
                rel.append(cur)
                temp = []
                temp.append(words[i])  因为当前的单词超长度了 所以处理后再加入
                lentemp = len(words[i])
            i += 1         #遍历下一个
        if temp != []:          #循环完后 如果temp还有  说明最后一行未满 左对齐 加空格即可
            cur = ''
            for each in temp:
                cur += each
            cur+=' '*(maxWidth-len(cur))
            rel.append(cur)
        return rel   

作者:yizhu-jia
链接:https://leetcode-cn.com/problems/text-justification/solution/mo-mo-mo-zi-ji-xiang-chu-lai-de-kun-nan-4fxuv/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

 

 

69   实现平方根  采用方法是二分查找   遍历可太难了 

        l = 0
        r = x
        ans = 0
        while(l<=r):
            mid = (l+r)//2
            if mid *mid >x:
                r = mid-1
            elif mid * mid <x:
                ans = mid
                l = mid+1
            else:
                    return mid
        return ans

70 :    爬楼梯  

简单的DP  但是我还是被提醒才知道用DP的 

class Solution:
    def climbStairs(self, n: int) -> int:
        if n == 1:
            return 1
        dp = [1]*n
        dp[0] = 1
        dp[1] = 2
        for i in range(2,n):
            dp[i] = dp[i-1]+dp[i-2]
        return dp[n-1]

作者:yizhu-jia
链接:https://leetcode-cn.com/problems/climbing-stairs/solution/zui-jian-dan-de-dpliao-by-yizhu-jia-3ldu/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 71: 简化路径

思路就是找到两个双斜杠之间的字符串进行判断
用rel记录目录
并用index指向当前所在的目录下标

代码

class Solution:
    def simplifyPath(self, path: str) -> str:
        def findnext(path,i):
            if i > n:
                return -1
            for index,each in enumerate(path[i:]):
                if each == '/':
                    return index+i
            return 3002               #找到下个斜杠的下标    如果后面没有了 就返回3002 因为字符串最长3000
        rel = []
        i = 0
        n = len(path)
        index = 0            #index为0表示主目录
        while i < n :
            pos1 = findnext(path,i)
            pos2 = findnext(path,i+1)
            cur = path[pos1+1:pos2]
            if cur == '.':        #是.或者连续两个 就什么都不做
                pass
            elif cur == '':
                pass
            elif cur == '..':    是..就回到上一级 
                index -= 1
                if index < 0:
                    index = 0
            else:        是目录 就加入牡蛎 
                if index < len(rel):
                    rel[index] = cur
                else:
                    rel.append(cur)
                index += 1
            i = pos2
        rel2 = ''
        if index <= 0:     #已经回到根目录了 
            return '/' 
        for each in rel[:index]:    #不在根目录 就全写下来 
            rel2 =rel2+ '/'+ each
        return rel2


作者:yizhu-jia
链接:https://leetcode-cn.com/problems/simplify-path/solution/jing-ran-bei-da-duo-shu-ji-bai-wo-shi-me-wcj1/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

72编辑距离    

看不懂答案的动态规划

class Solution:
    def minDistance(self, word1: str, word2: str) -> int:

        n1 = len(word1)+1
        n2 = len(word2)+1
        dp = [[100]*(n1) for j in range(n2)]
        for i in range(n1):
            dp[0][i] = i
        for j in range(n2):
            dp[j][0] = j
        for i in range(1,n2):
            for j in range(1,n1):
                if word1[j-1] == word2[i-1]:
                    dp[i][j] = dp[i-1][j-1]
                else:
                    dp[i][j]  = min(dp[i-1][j-1],dp[i-1][j],dp[i][j-1])+1
        return dp[n2-1][n1-1]


作者:yizhu-jia
链接:https://leetcode-cn.com/problems/edit-distance/solution/wu-wu-wu-wu-uwu-chu-kui-dpmen-lu-by-yizh-col9/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

posted @ 2021-11-01 16:38  yi术家  阅读(48)  评论(0)    收藏  举报