generating Variations without repetitions / Permutations in java

后端 未结 10 1110
半阙折子戏
半阙折子戏 2021-01-02 07:33

I have to generate all variations without repetitions made of digits 0 - 9.

Length of them could be from 1 to 10. I really don\'t know how to solve it, especially ho

10条回答
  •  滥情空心
    2021-01-02 07:55

    def find(alphabet, alpha_current, str, str_current, max_length, acc):
      
      if (str_current == max_length):
        acc.append(''.join(str))
        return
      
      for i in range(alpha_current, len(alphabet)):
        str[str_current] = alphabet[i]
        
        alphabet[i], alphabet[alpha_current] = alphabet[alpha_current], alphabet[i]
        
        find(alphabet, alpha_current+1, str, str_current+1, max_length, acc)
        
        alphabet[i], alphabet[alpha_current] = alphabet[alpha_current], alphabet[i]
      
      return
    
    max_length = 4
    str = [' ' for i in range(max_length)]
    acc = list()
    find(list('absdef'), 0, str, 0, max_length, acc)
    
    for i in range(len(acc)):
      print(acc[i])
    
    print(len(acc))
    

提交回复
热议问题