Different result between Eclipse and the server

  • 0

    Here is my code. When I submitted it, the system said:
    Input: 5
    Output: 9
    Expected: 10

    But when I try it on my own computer with Eclipse, the result for totalNQueens(5) is 10.

    class Solution {
        int totalNQueenSub(int cur, int n, int pos[], int sum){
    		int newsum = sum;
    		bool *record = new bool[n];
    		for(int j=0;j<cur;j++){
    			int diff = cur - j;
    			if (pos[j]-diff>=0) record[pos[j]-diff] = true;
    			if (pos[j]+diff<n) record[pos[j]+diff] = true;
    			record[pos[j]] = true;
    		for(int i=0;i<n;i++){
    			pos[cur] = i;
    			if (!record[i]){
    				if (cur+1==n)
    					return sum+1;
    				newsum = totalNQueenSub(cur+1, n, pos, newsum);
    		return newsum;
    	int totalNQueens(int n){
            int *pos = new int[n];
            return totalNQueenSub(0, n, pos, 0);


  • 0

    same problem and solved with 2d array(size of 50) instead of dynamic array

Log in to reply

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