zoukankan      html  css  js  c++  java
  • 全排列算法实现

    def ascii_permutation(arr:list):
        length = len(arr)
    
        while(True):
            print(arr)
    
            for i in range(length-1,-1, -1):
                if arr[i-1] < arr[i]:
                    x = i-1
                    break
    
            if i == 0:
                return
    
            for j in range(length-1, x, -1):
                if arr[j] > arr[x]:
                    y = j
                    break
    
    
            arr[x], arr[y] = arr[y], arr[x]
            sub = arr[x+1:]
            sub.reverse()
            arr = arr[0:x+1] + sub
    
    def recursive_permutation(arr:list, start:int, end:int):
        if start == end:
            print(arr)
    
        else:
            for i in range(start, end):
                arr[i], arr[start] = arr[start], arr[i]
                recursive_permutation(arr, start+1, end)
                arr[i], arr[start] = arr[start], arr[i]
    
    #arr = [1,2,3,4]
    
    #ascii_permutation(arr)
    #print('****************')
    #recursive_permutation(arr,0,len(arr))
    

      

  • 相关阅读:
    iptables
    apt
    cvc-elt.1: Cannot find the declaration of element 'beans'.
    di
    log
    java内存模型
    spring-jms
    JTS
    10java进阶——IO2
    17单例
  • 原文地址:https://www.cnblogs.com/panchong/p/11038308.html
Copyright © 2011-2022 走看看