Help ! why my code get Runtime Error in leetcode but it can return right answer on my PC?


  • 0
    J

    my code is as follows:

    class Solution {
        vector<int> a;
    
        int check(int i)
        {
            for(int j=0; j<i; j++)
            {
                int d = a[i] - a[j];
                if(d==0 || d == i-j || d == j-i)
                    return 1;
            }
            return 0;
        }
    
    public:
        int totalNQueens(int n) {
            for(auto i=0; i<n; i++)
                a.push_back(0);
    
            int sum=0, i=0;
            while(i >= 0)
            {
                for(; a[i] < n; a[i]++)
                {
                    if(check(i)==0)
                        break;
                }
    
                if(a[i] < n)
                {
                    if(i==n-1)
                    {
                        sum++;// one solution
                        i--;
                        a[i]++;
                    } else {
                        i++;
                        a[i] = 0;
                    }
                } else {
                    i--;
                    a[i]++;
                }
            }
            return sum;
        }
    
    };

Log in to reply
 

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