diff --git a/Leetcode/CPP/52. N-Queens II.cpp b/Leetcode/CPP/52. N-Queens II.cpp new file mode 100644 index 00000000..2f8c0ef1 --- /dev/null +++ b/Leetcode/CPP/52. N-Queens II.cpp @@ -0,0 +1,58 @@ +//problem link:https://leetcode.com/problems/n-queens-ii/ +//Time complexity : best and average case O(n*n) +//worst case:O(n!) +class Solution { +public: + bool issafe(vector &board,int n,int row,int col) +{ + for(int i=0;i=0 && j>=0; i--,j--) + { + if(board[i][j]=='Q') + { + return false; + } + } + for(int i=row,j=col; i=0; i++,j--) + { + if(board[i][j]=='Q') + { + return false; + } + } + return true; +} +void nqueen(vector> &ans,vector &board,int n,int col) +{ + if(col==n) + { + ans.push_back(board); + } + for(int row=0;row>ans; + vectorboard(n); + string s(n,'.'); + for (int i = 0; i < n; i++) + { + board[i]=s; + } + nqueen(ans,board,n,0); + return ans.size(); + } +}; \ No newline at end of file