leetcode 78 子集 1411. 给 N x 3 网格图涂色的方案数

Nice 哦,这次一下就想出来了,我真棒!

class Solution(object):
    def subsets(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        result = []
        for i in range(pow(2,len(nums))):
            sub_result = []
            li = [i>>k & 1 for k in range(len(nums))][::-1]
            for j in range(len(li)):
                if li[j]==1:
                    sub_result.append(nums[j])
            result.append(sub_result)
        return result

自带库:

result = []
        for i in range(len(nums)+1):
            for tmp in itertools.combinations(nums,i):
                result.append(tmp)
        return result
  1. 给 N x 3 网格图涂色的方案数
class Solution(object):
    def numOfWays(self, n):
        """
        :type n: int
        :rtype: int
        """
        dp=[[0]*2 for i in range(n)]
        dp[0][0]=6
        dp[0][1]=6
        for i in range(1,n):
            dp[i][0]=dp[i-1][0]*3+dp[i-1][1]*2
            dp[i][1]=dp[i-1][0]*2+dp[i-1][1]*2
        return (dp[n-1][0]+dp[n-1][1])%1000000007
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页