LeetCode and Python Shell gives me different results for the same input.


  • 0
    J
    class Node:
    def __init__(self):
        self.mark = None
        self.child = []
    
    flag = 1
    class Solution:
    def canFinish(self, numCourses, prerequisites):
        graphNode = []
        if len(prerequisites)<1:
            return True                                       #no edge return true
        for i in range (numCourses):
            graphNode.append(Node())
        for item in prerequisites:
            if item[0] == item[1]:                         #(1,1) gives a False
                return False
            else:
                graphNode[item[1]].child.append(item[0])
        def visit(num):                                     #DFS search
            global flag
            if graphNode[num].mark == 0:
                flag = 0
                return
            if graphNode[num].mark == None:
                graphNode[num].mark = 0
                for item in graphNode[num].child:
                    visit(item)
                graphNode[num].mark = 1
        global flag
        for i in range(numCourses):
            if graphNode[i].mark ==None:
                visit(i)
        if flag==1:
            return True
        else:
            return False
    

    This implementation is based on DFS. When the input is (3,[[1,0]]), system says my code gives "False" while the correct answer should be "True".

    However, when I run the code in Python Shell given the same input, it gives "True".

    Does any one know why this would happen?


Log in to reply
 

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