{"id":5461,"date":"2019-08-20T14:33:04","date_gmt":"2019-08-20T21:33:04","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5461"},"modified":"2019-08-20T14:33:33","modified_gmt":"2019-08-20T21:33:33","slug":"leetcode-36-valid-sudoku","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-36-valid-sudoku\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 36. Valid Sudoku"},"content":{"rendered":"\n<p>Determine if a&nbsp;9&#215;9 Sudoku board&nbsp;is valid.&nbsp;Only the filled cells need to be validated&nbsp;<strong>according to the following rules<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Each row&nbsp;must contain the&nbsp;digits&nbsp;<code>1-9<\/code>&nbsp;without repetition.<\/li><li>Each column must contain the digits&nbsp;<code>1-9<\/code>&nbsp;without repetition.<\/li><li>Each of the 9&nbsp;<code>3x3<\/code>&nbsp;sub-boxes of the grid must contain the digits&nbsp;<code>1-9<\/code>&nbsp;without repetition.<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/thumb\/f\/ff\/Sudoku-by-L2G-20050714.svg\/250px-Sudoku-by-L2G-20050714.svg.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><br>A partially filled sudoku which is valid.<\/p>\n\n\n\n<p>The Sudoku board could be partially filled, where empty cells are filled with the character&nbsp;<code>'.'<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong>\n[\n  [\"5\",\"3\",\".\",\".\",\"7\",\".\",\".\",\".\",\".\"],\n  [\"6\",\".\",\".\",\"1\",\"9\",\"5\",\".\",\".\",\".\"],\n  [\".\",\"9\",\"8\",\".\",\".\",\".\",\".\",\"6\",\".\"],\n  [\"8\",\".\",\".\",\".\",\"6\",\".\",\".\",\".\",\"3\"],\n  [\"4\",\".\",\".\",\"8\",\".\",\"3\",\".\",\".\",\"1\"],\n  [\"7\",\".\",\".\",\".\",\"2\",\".\",\".\",\".\",\"6\"],\n  [\".\",\"6\",\".\",\".\",\".\",\".\",\"2\",\"8\",\".\"],\n  [\".\",\".\",\".\",\"4\",\"1\",\"9\",\".\",\".\",\"5\"],\n  [\".\",\".\",\".\",\".\",\"8\",\".\",\".\",\"7\",\"9\"]\n]\n<strong>Output:<\/strong> true\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong>\n[\n&nbsp; [\"8\",\"3\",\".\",\".\",\"7\",\".\",\".\",\".\",\".\"],\n&nbsp; [\"6\",\".\",\".\",\"1\",\"9\",\"5\",\".\",\".\",\".\"],\n&nbsp; [\".\",\"9\",\"8\",\".\",\".\",\".\",\".\",\"6\",\".\"],\n&nbsp; [\"8\",\".\",\".\",\".\",\"6\",\".\",\".\",\".\",\"3\"],\n&nbsp; [\"4\",\".\",\".\",\"8\",\".\",\"3\",\".\",\".\",\"1\"],\n&nbsp; [\"7\",\".\",\".\",\".\",\"2\",\".\",\".\",\".\",\"6\"],\n&nbsp; [\".\",\"6\",\".\",\".\",\".\",\".\",\"2\",\"8\",\".\"],\n&nbsp; [\".\",\".\",\".\",\"4\",\"1\",\"9\",\".\",\".\",\"5\"],\n&nbsp; [\".\",\".\",\".\",\".\",\"8\",\".\",\".\",\"7\",\"9\"]\n]\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> Same as Example 1, except with the <strong>5<\/strong> in the top left corner being \n    modified to <strong>8<\/strong>. Since there are two 8's in the top left 3x3 sub-box, it is invalid.\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A Sudoku board (partially filled) could be valid but is not necessarily solvable.<\/li><li>Only the filled cells need to be validated according to the mentioned&nbsp;rules.<\/li><li>The given board&nbsp;contain only digits&nbsp;<code>1-9<\/code>&nbsp;and the character&nbsp;<code>'.'<\/code>.<\/li><li>The given board size is always&nbsp;<code>9x9<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: HashTable<\/strong><\/h2>\n\n\n\n<p>Use hashtable to store the numbers of each row, column and each 3&#215;3 box. If there number appears more than once then it&#8217;s an invalid Sudoku. <\/p>\n\n\n\n<p>Time complexity: O(1)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool isValidSudoku(vector<vector<char> > &board) {        \n    for (int i = 0; i < 9;i++) {\n      set<char> r, c;\n      for (int j = 0; j < 9; j++) {\n        if (board[i][j] != '.' &#038;&#038; !r.insert(board[i][j]).second) \n          return false;\n        if (board[j][i] != '.' &#038;&#038; !c.insert(board[j][i]).second)\n          return false;\n      }\n    }\n\n    for (int p = 0; p < 3; p++)\n      for (int q = 0; q < 3; q++) {\n        set<char> b;\n        for (int i = 0; i < 3; i++)\n          for (int j = 0; j < 3; j++) {\n            int x = p * 3 + i;\n            int y = q * 3 + j;\n            if (board[y][x] != '.' &#038;&#038; !b.insert(board[y][x]).second)\n              return false;\n          }\n        }\n    return true;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Follow up:<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-37-sudoku-solver\/\">https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-37-sudoku-solver\/<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Determine if a&nbsp;9&#215;9 Sudoku board&nbsp;is valid.&nbsp;Only the filled cells need to be validated&nbsp;according to the following rules: Each row&nbsp;must contain the&nbsp;digits&nbsp;1-9&nbsp;without repetition. Each column must&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[82,177,489],"class_list":["post-5461","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hashtable","tag-medium","tag-sudoku","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5461","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5461"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5461\/revisions"}],"predecessor-version":[{"id":5463,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5461\/revisions\/5463"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}