当前位置:网站首页>Day29-t77 & t1726-2022-02-13-don't answer by yourself

Day29-t77 & t1726-2022-02-13-don't answer by yourself

2022-07-06 08:11:00 Parchment

77
Given two integers n and k, Return range [1, n] All the possibilities in k Combination of numbers .

You can press In any order Return to the answer .

class Solution:
    def combine(self, n: int, k: int) -> List[List[int]]:
        ans = list()
        t = list()
        def find_all(i,j):
            if j == k:
                ans.append(t[:])
            else:
                if i <= n- k + j + 1:
                    for a in range(i,n+1):
                        t.append(a)
                        find_all(a+1,j+1)
                        t.pop()
        find_all(1,0)
        return ans

#  author :haotianmingyue
#  link :https://leetcode-cn.com/problems/combinations/solution/python-hui-su-by-haotianmingyue-0jav/

1726 Coproduct tuple
Here you are Different An array of positive integers nums , Please return to contentment a * b = c * d tuples (a, b, c, d) The number of . among a、b、c and d All are nums The elements in , And a != b != c != d .

source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/tuple-with-same-product

class Solution:
    def tupleSameProduct(self, nums: List[int]) -> int:
        n=len(nums)
        freq = Counter([nums[i]*nums[j] for i in range(n) for j in range(i+1,n)])
        cnt=0
        for prod in freq:
            if freq[prod]>0:
                cnt+=freq[prod]*(freq[prod]-1)//2*8
        return cnt

#  author :yuer-flyfly
#  link :https://leetcode-cn.com/problems/tuple-with-same-product/solution/ha-xi-biao-pai-lie-zu-he-tong-ji-yuan-zu-5q0v/
原网站

版权声明
本文为[Parchment]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131839507799.html

随机推荐