Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does python find out the number of combinations?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to find out the number of combinations in python". The editor shows you the operation process through an actual case. The operation method is simple and fast, and it is practical. I hope this article "how to find out the number of combinations in python" can help you solve the problem.

Topic: combination summation

Given an array of positive integers with no duplicate numbers, find out the number of combinations of positive integers with a given target.

Example:

Nums = [1,2,3]

Target = 4

All possible combinations are:

(1, 1, 1, 1)

(1, 1, 2)

(1, 2, 1)

(1, 3)

(2, 1, 1)

(2, 2)

(3, 1)

Note that sequences with different orders are treated as different combinations.

So the output is 7.

Solve the problem:

1. Dp problem. Dp [I] = sum (DP [I-nums [j])), where nums [j]

< i。 代码 class Solution: def combinationSum4(self, nums: List[int], target: int) ->

Int:

Nums.sort ()

Dp = [0] * (target + 1)

For i in range (1, target + 1):

For n in nums:

If n > I:

Break

If n = = I:

Dp [I] + = 1

Else:

Dp [I] + = dp [I-n]

Return dp [- 1]

This is the end of the content about "how to find out the number of combinations by python". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report