其他分享
首页 > 其他分享> > 43-47 这周又有几道题要谢罪

43-47 这周又有几道题要谢罪

作者:互联网

43:  字符串相乘  我以为是一道很简单的题  

不让用int 就用ASCII嘛  结果 原来自己是沾了python的光

这题是大数 所以只有python的整数可以放下这么大的数   因此其他语言这种方法是不可行的

class Solution(object):
    def multiply(self, num1, num2):
        """
        :type num1: str
        :type num2: str
        :rtype: str
        """
        def getnum(strnum):
            num = 0
            for each in strnum:
                num = (ord(each)-ord('0'))+num*10
            return num
        return str(getnum(num1)*getnum(num2))

作者:yizhu-jia
链接:https://leetcode-cn.com/problems/multiply-strings/solution/strneng-yong-ma-by-yizhu-jia-tnlm/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

44: 通配符匹配  借鉴了上次那个正则匹配  开心的是 这个动态规划我竟然一次就写对了  耶!

class Solution(object):
    def isMatch(self, s, p):
        """
        :type s: str
        :type p: str
        :rtype: bool
        """
        row = len(s)+1
        col = len(p)+1
        dp = [[False]*(col) for i in range (row)]
        dp[0][0] = True
        for j in range(1,col):
            if p[j-1] == '*':
                dp[0][j] = dp[0][j-1]
        for i in range(1,row):
            for j in range(1,col):
                if p[j-1] == '*':
                    if dp[i][j-1] == True or dp[i-1][j-1] == True or dp[i-1][j]==True:
                        dp[i][j] = True
                elif p[j-1] == '?':
                        dp[i][j] = dp[i-1][j-1]
                else:
                    if p[j-1] == s[i-1]:
                        dp[i][j] = dp[i-1][j-1]
        return dp[row-1][col-1]


作者:yizhu-jia
链接:https://leetcode-cn.com/problems/wildcard-matching/solution/wo-ca-ca-ca-ca-ca-dong-tai-gui-hua-wo-ji-jmpu/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

45:

跳跃游戏  我第一反应就是回溯法,,,,然而证明我是错的  回溯直接超时了 对不起 

看了答案  答案不愧是答案  维持几个指针  分别指向当前可以跳到最远的位置 和 此跳的最后位置

相当于在每一跳之前 先找到下跳能跳的最远位置 

class Solution(object):
    def jump(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """       
        # def DFS(curpos,count,minjump):
        #     if count > minjump:
        #         return minjump
        #     if curpos == n-1:
        #         minjump = count
        #     else:
        #         for i in range(1,nums[curpos]+1):
        #             if curpos+i < n:
        #                 curjump = DFS(curpos+i,count+1,minjump)
        #                 if curjump < minjump:
        #                     minjump = curjump
        #     return minjump
        # n = len(nums)
        # minjump = n
        # return(DFS(0,0,minjump))

        curjump = 0
        step = 0
        n = len(nums)
        maxpos = 0
        for i in range(n-1):
                maxpos = max(maxpos,i+nums[i])
                if i == curjump:
                    curjump = maxpos
                    step += 1
        return step



作者:yizhu-jia
链接:https://leetcode-cn.com/problems/jump-game-ii/solution/ke-lian-de-hui-su-fa-zhi-jie-yi-ge-shi-j-516j/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

46:全排列  :普通的回溯 普通的一天 

class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
        path = []
        rel = []
        n = len(nums)
        def DFS(level,path,rel):
            if level == n:
                rel.append(path[:])
            for each in nums:
                if each not in path:
                    path.append(each)
                    DFS(level+1,path,rel)
                    path.pop()
        DFS(0,path,rel)
        return rel

作者:yizhu-jia
链接:https://leetcode-cn.com/problems/permutations/solution/pu-tong-de-hui-su-pu-tong-de-yi-tian-by-ajly3/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

47:非重复全排列  想了半天想不到啥好方法。。。直接一个去重的去重

class Solution:
    def permuteUnique(self, nums: List[int]) -> List[List[int]]:

        path = []
        rel = []
        n = len(nums)
        visted= [False] * n
        def DFS(level,path,rel):
            if level == n:
                rel.append(path[:])
            for index,each in enumerate(nums):
                if visted[index] == False:
                    path.append(each)
                    visted[index] = True
                    DFS(level+1,path,rel)
                    path.pop()
                    visted[index] = False
        DFS(0,path,rel)
        rel = sorted(rel)
        rel2 = [rel[0]]
        for i in range(1,len(rel)):
            if rel[i] != rel[i-1]:
                rel2.append(rel[i])
        return rel2

作者:yizhu-jia
链接:https://leetcode-cn.com/problems/permutations-ii/solution/dui-bu-qi-qu-zhong-cai-yong-liao-ben-fan-mmxc/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

标签:return,nums,47,题要,43,rel,path,dp,minjump
来源: https://www.cnblogs.com/xiaoli1996/p/15335315.html