Compile Error,but code works well on my machine


  • 0
    O
    import math
    class Solution:
        @classmethod
        def check(cls,stack,row,col):
            for i in range(0,row,1):
                if stack[i] == col or math.fabs(stack[i] -col) == row - i:
                    return False
    
            return True
    
        @classmethod
        def show(cls,stack,n):
            #print stack
            resl = []
            for i in range(n):
                resl.append("")
                for j in range(n):
                    if stack[i] == j:
                        resl[i] = resl[i]+'Q'
                    else:
                        resl[i] = resl[i]+'.'
    
            return resl
    
    
        # @return a list of lists of string
        def solveNQueens(self, n):
            res = []
            stack = []
            stack.append(0)
            start = 0
            while True:
                sl = len(stack)
                if sl < n:
                    for i in range(start,n,1):
                        if Solution.check(stack, len(stack), i):
                            stack.append(i)
                            start =0
                            break
    
                    if sl == len(stack):
                        if sl == 0:
                            return res
                        else:
                            start = stack.pop() + 1
                    else:
                        pass
    
                else:
                    res.append(Solution.show(stack, n))
                    start = stack.pop() + 1

  • 0
    O

    I have tested it on my machine for n=4 to 14,nothing wrong.But when I try to submit it,I get Compile Error.Can anybody help me with that?Any advice will be appreciated!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.