BUG IN LEETCODE OF THIS QUESTION?!


  • 0
    E

    Local running result is the same as the 'run code' of leetcode, but when submitting, the result is changed??!!

    Waiting for answering...
    here is the different result of 'run code' and 'submit'

    Run Code Result:
    Your input: [1,2,2,2]
    Your answer: false
    Expected answer: false

    Submission Result: Wrong Answer
    Input:[1,2,2,2]
    Output:true
    Expected:false

    here is my code:

    bool isCrossing(int axis[][2],int axis_ple[][2],int flag);
    bool isSelfCrossing(int* x, int xSize) {
    	if (xSize<4) return 0;
    	int axis[5][2]={0};
    	int axis_ple[2][2]={0};
    	int xt=0,yt=0;
    	axis[0][0]=xt;
    	axis[0][1]=yt;
    	axis_ple[0][0]=xt;
    	axis_ple[0][1]=yt;
    	int flag=0;
    	for (int i=0;i<xSize;i++)
    	{
            if (i%4==0) yt=yt+x[i];
            else if (i%4==1) xt=xt-x[i];
            else if (i%4==2) yt=yt-x[i];
            else if (i%4==3) xt=xt+x[i];
    
    		if (i==0){axis[1][0]=xt;axis[1][1]=yt;axis_ple[1][0]=xt;axis_ple[1][1]=yt;}
    		else if (i==1){axis[2][0]=xt;axis[2][1]=yt;}
    		else if (i==2){axis[3][0]=xt;axis[3][1]=yt;} 
    		if (i>2) 
    		{
    			axis[4][0]=xt;axis[4][1]=yt;
    			if (i==3 && axis[4][0]==0)
    			{
    				if (x[i+1]+axis[4][1]>=0) return 1;
    			}
    			if (i>=5) flag=1;
    			if (isCrossing(axis,axis_ple,flag)) return 1;
    		}
    	}
    	return 0;
    }
    bool isCrossing(int axis[][2],int axis_ple[][2],int flag)
    {
    	if (axis[0][0]==axis[1][0])
    	{
    		if ((axis[3][1]-axis[0][1])*(axis[3][1]-axis[1][1])<=0 && (axis[0][0]-axis[3][0])*(axis[0][0]-axis[4][0])<=0) return 1;
    	}
    	if (axis[0][1]==axis[1][1])
    	{
    		if ((axis[3][0]-axis[0][0])*(axis[3][0]-axis[1][0])<=0 && (axis[0][1]-axis[3][1])*(axis[0][1]-axis[4][1])<=0) return 1;
    	}
    	if (flag)
    	{
    		if (axis_ple[0][0]==axis_ple[1][0])
    		{
    			if ((axis[3][1]-axis_ple[0][1])*(axis[3][1]-axis_ple[1][1])<=0 && (axis_ple[0][0]-axis[3][0])*(axis_ple[0][0]-axis[4][0])<=0) return 1;
    		}
    		if (axis_ple[0][1]==axis_ple[1][1])
    		{
    			if ((axis[3][0]-axis_ple[0][0])*(axis[3][0]-axis_ple[1][0])<=0 && (axis_ple[0][1]-axis[3][1])*(axis_ple[0][1]-axis[4][1])<=0) return 1;
    		}
    		axis_ple[0][0]=axis_ple[1][0];
    		axis_ple[0][1]=axis_ple[1][1];
    		axis_ple[1][0]=axis[0][0];
    		axis_ple[1][1]=axis[0][1];
    	}
    	for (int i=0;i<4;i++)
    	{
    		axis[i][0]=axis[i+1][0];
    		axis[i][1]=axis[i+1][1];
    	}
    	return 0;
    }

  • 0

    Please read the FAQ.

    If I were you, I would check carefully if there is any array out of bound access. If that happens, your code could result in undefined behavior.


  • 0
    Q

    Yep it's out of bound access on line

    if (x[i+1]+axis[4][1]>=0) return 1;


  • 0
    E

    That's it!! Thanks!


Log in to reply
 

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