数字不应该从零开始。
示例:
鉴于123,ans = 3! = 6 [123,132,213,231,312,321]
鉴于1122,ans = 4!/ 2! = 6 [1122,1212,1221,2211,2121,2112]
给定100,ans = 1 [010,001不允许]
我被要求编写一个java代码来解决这个问题。 我没有形成处理零的通用算法。请帮我解决问题和一些阅读材料。
答案 0 :(得分:2)
编写一个找到组合数的函数。假设它被称为 整数com(列表编号);要实现这一点,请阅读:https://en.wikipedia.org/wiki/Combination
现在要解决这个问题,首先找到一个com(listOfNumbers)可以找到的总数。然后找到开头有0的组合数,然后减去它。
例如:
1122
Total combinations possible com(1122) = 4! / (2! X 2!)
Total numbers starting with 0 = 0
So ans = 4! / (2! X 2!)
001122
Total combinations possible com(001122) = 6! / (2! X 2! X 2!)
Total numbers starting with 0
= fix one 0 in the beginning and find all possible combination for the rest of the numbers
= com(01122) = 5! / (2! X 2!)
So ans = com(001122) - com(01122)
001
Total combinations possible com(001) = 3! / (2!) = 3
Total numbers starting with 0
= fix one 0 in the beginning and find all possible combination for the rest of the numbers
= com(01) = 2! = 2
So ans = com(001) - com(01) = 1