Skip to content Navigation Menu Toggle navigation Sign in Appearance settings PlatformAI CODE CREATIONGitHub CopilotWrite better code with AIGitHub Copilot appDirect agents from issue to mergeMCP RegistryNewIntegrate external toolsDEVELOPER WORKFLOWSActionsAutomate any workflowCodespacesInstant dev environmentsIssuesPlan and track workCode ReviewManage code changesAPPLICATION SECURITYGitHub Advanced SecurityFind and fix vulnerabilitiesCode securitySecure your code as you buildSecret protectionStop leaks before they startEXPLOREWhy GitHubDocumentationBlogChangelogMarketplaceView all featuresSolutionsBY COMPANY SIZEEnterprisesSmall and medium teamsStartupsNonprofitsBY USE CASEApp ModernizationDevSecOpsDevOpsCI/CDView all use casesBY INDUSTRYHealthcareFinancial servicesManufacturingGovernmentView all industriesView all solutionsResourcesEXPLORE BY TOPICAISoftware DevelopmentDevOpsSecurityView all topicsEXPLORE BY TYPECustomer storiesEvents & webinarsEbooks & reportsBusiness insightsGitHub SkillsSUPPORT & SERVICESDocumentationCustomer supportCommunity forumTrust centerPartnersView all resourcesOpen SourceCOMMUNITYGitHub SponsorsFund open source developersPROGRAMSSecurity LabMaintainer CommunityAcceleratorGitHub StarsArchive ProgramREPOSITORIESTopicsTrendingCollectionsEnterpriseENTERPRISE SOLUTIONSEnterprise platformAI-powered developer platformAVAILABLE ADD-ONSGitHub Advanced SecurityEnterprise-grade security featuresCopilot for BusinessEnterprise-grade AI featuresPremium SupportEnterprise-grade 24/7 supportPricing Search or jump to... Search code, repositories, users, issues, pull requests... Search Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. Include my email address so I can be contacted Saved searches Use saved searches to filter your results more quickly Name Query To see all available qualifiers, see our documentation. Sign in Sign up Appearance settings Resetting focus You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} rajeevranjancom / Leetcode_Java Public Notifications You must be signed in to change notification settings Fork 0 Star 0 Code Issues 0 Pull requests 0 Actions Projects Security and quality 0 Insights Additional navigation options Code Issues Pull requests Actions Projects Security and quality Insights FilesExpand file tree masterBreadcrumbsLeetcode_Java/052_NQueensII52.javaCopy pathBlameMore file actionsBlameMore file actions Latest commit HistoryHistoryHistory54 lines (46 loc) · 1.66 KB masterBreadcrumbsLeetcode_Java/052_NQueensII52.javaCopy pathTopFile metadata and controlsCodeBlame54 lines (46 loc) · 1.66 KBRawCopy raw fileDownload raw fileOpen symbols panelEdit and raw actions123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354/** * Follow up for N-Queens problem. * * Now, instead outputting board configurations, return the total number of * distinct solutions. */ import java.util.Arrays;import java.util.HashMap;import java.util.Map; public class NQueensII52 { private void helper(int r, boolean[] cols, boolean[] d1, boolean[] d2, int n, Map<String, Integer> res) { if (r == n) { res.put("num", res.get("num") + 1); } else { for (int c = 0; c < n; c++) { int id1 = r - c + n, id2 = 2*n - r - c - 1; if (!cols[c] && !d1[id1] && !d2[id2]) { cols[c] = true; d1[id1] = true; d2[id2] = true; helper(r+1, cols, d1, d2, n, res); cols[c] = false; d1[id1] = false; d2[id2] = false; } } } } public int totalNQueens(int n) { Map<String, Integer> res = new HashMap<>(); res.put("num", 0); helper(0, new boolean[n], new boolean[2*n], new boolean[2*n], n, res); return res.get("num"); } public static void main(String[] args) { NQueensII52 nq = new NQueensII52(); System.out.println("---- 1"); System.out.println(nq.totalNQueens(1)); System.out.println("---- 4"); System.out.println(nq.totalNQueens(4)); System.out.println("---- 2"); System.out.println(nq.totalNQueens(2)); System.out.println("---- 3"); System.out.println(nq.totalNQueens(3)); System.out.println("---- 5"); System.out.println(nq.totalNQueens(5)); System.out.println("---- 6"); System.out.println(nq.totalNQueens(6)); }} You can’t perform that action at this time.