Why do I get different result compared to my local machine?


  • 0
    R
    class Solution {
    public:
    int Min(int x,int y)
    {
        if(x<y) return x;
        return y;
    }
    int trap(int a[], int n)
    {
    	int count=0,i,start,j;
    	bool trap;
    	//while(1)
    	//{
    		i=1;
    	//	trap=false;
    		while(i<=n-2)
    		{
    			while(a[i-1]>a[i] && a[i]<=a[i+1])
    			{
    				start=i;
    				while(i<=n-2 && a[i]==a[i+1])
    					i++;
    				if(a[i]<a[i+1])
    				{
    					count+=(i-start+1)*(Min(a[start-1],a[i+1])-a[start]);
    					//trap=true;
    					for(j=start;j<=i;j++)
    						a[j]=Min(a[start-1],a[i+1]);
    					
    					j=start;
    					while(j>=1 && a[j-1]==a[j])
    						j--;
    					if(j>=1)
    					{
    						if(a[j-1]>a[j])
    							i=j;
    						else
    							break;
    					}
    					else
    						;
    				}
    				else
    					;
    			}
    			//else
    			//	;
    			i++;
    		}
    	//	if(trap==false)
    	//		break;
    	//}
    	return count;
    }
    };
    

    I use one pass algorithm, and use Min in case that leetcode do not support C++11, but when in case [4,2,3], my local get 1, which is correct, but leetcode OJ said I print out 3, which is not correct, could anybody help me?


  • 0
    R

    I have described it clearly........


  • 0
    S

    There is no comment in code or any detail thought about it.


  • 0
    R

    I have found my error, I use MSVC to compile, while in this code, gcc get different result, but currently I do not know which part are different between MSVC and GCC compilor, could you help me thanks


  • 0
    R

    I have solved it, it's my code bug, and MSVC is bad......


Log in to reply
 

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