相关疑难解决方法(0)

找到格式正确的括号的所有组合

这是在与朋友交谈时提出的,我想我会问这里,因为这是一个有趣的问题,并希望看到其他人的解决方案.

任务是编写一个函数Brackets(int n),它打印1 ... n 中格式正确的括号的所有组合.对于Brackets(3),输出将是

()
(())  ()()   
((()))  (()())  (())()  ()(())  ()()()
Run Code Online (Sandbox Code Playgroud)

c# algorithm f# catalan

32
推荐指数
5
解决办法
2万
查看次数

Python中的N-queen回溯:如何返回解决方案而不是打印它们?

def solve(n):
    #prepare a board
    board = [[0 for x in range(n)] for x in range(n)]
    #set initial positions
    place_queen(board, 0, 0)

def place_queen(board, row, column):
    """place a queen that satisfies all the conditions"""
    #base case
    if row > len(board)-1:
        print board
    #check every column of the current row if its safe to place a queen
    while column < len(board):
        if is_safe(board, row, column):
            #place a queen
            board[row][column] = 1
            #place the next queen with an updated board
            return place_queen(board, …
Run Code Online (Sandbox Code Playgroud)

python recursion backtracking

6
推荐指数
1
解决办法
8021
查看次数

标签 统计

algorithm ×1

backtracking ×1

c# ×1

catalan ×1

f# ×1

python ×1

recursion ×1