<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>sudoku Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/sudoku/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/sudoku/</link>
	<description></description>
	<lastBuildDate>Tue, 20 Aug 2019 21:33:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>sudoku Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/sudoku/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 36. Valid Sudoku</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-36-valid-sudoku/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-36-valid-sudoku/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 20 Aug 2019 21:33:04 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[sudoku]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5461</guid>

					<description><![CDATA[<p>Determine if a&#160;9&#215;9 Sudoku board&#160;is valid.&#160;Only the filled cells need to be validated&#160;according to the following rules: Each row&#160;must contain the&#160;digits&#160;1-9&#160;without repetition. Each column must&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-36-valid-sudoku/">花花酱 LeetCode 36. Valid Sudoku</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<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>



<ol><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>



<figure class="wp-block-image"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png" alt=""/></figure>



<p><br>A partially filled sudoku which is valid.</p>



<p>The Sudoku board could be partially filled, where empty cells are filled with the character&nbsp;<code>'.'</code>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong>
[
  ["5","3",".",".","7",".",".",".","."],
  ["6",".",".","1","9","5",".",".","."],
  [".","9","8",".",".",".",".","6","."],
  ["8",".",".",".","6",".",".",".","3"],
  ["4",".",".","8",".","3",".",".","1"],
  ["7",".",".",".","2",".",".",".","6"],
  [".","6",".",".",".",".","2","8","."],
  [".",".",".","4","1","9",".",".","5"],
  [".",".",".",".","8",".",".","7","9"]
]
<strong>Output:</strong> true
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong>
[
&nbsp; ["8","3",".",".","7",".",".",".","."],
&nbsp; ["6",".",".","1","9","5",".",".","."],
&nbsp; [".","9","8",".",".",".",".","6","."],
&nbsp; ["8",".",".",".","6",".",".",".","3"],
&nbsp; ["4",".",".","8",".","3",".",".","1"],
&nbsp; ["7",".",".",".","2",".",".",".","6"],
&nbsp; [".","6",".",".",".",".","2","8","."],
&nbsp; [".",".",".","4","1","9",".",".","5"],
&nbsp; [".",".",".",".","8",".",".","7","9"]
]
<strong>Output:</strong> false
<strong>Explanation:</strong> Same as Example 1, except with the <strong>5</strong> in the top left corner being 
    modified to <strong>8</strong>. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
</pre>



<p><strong>Note:</strong></p>



<ul><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>



<h2><strong>Solution: HashTable</strong></h2>



<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>



<p>Time complexity: O(1)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  bool isValidSudoku(vector&lt;vector&lt;char&gt; &gt; &amp;board) {        
    for (int i = 0; i &lt; 9;i++) {
      set&lt;char&gt; r, c;
      for (int j = 0; j &lt; 9; j++) {
        if (board[i][j] != '.' &amp;&amp; !r.insert(board[i][j]).second) 
          return false;
        if (board[j][i] != '.' &amp;&amp; !c.insert(board[j][i]).second)
          return false;
      }
    }

    for (int p = 0; p &lt; 3; p++)
      for (int q = 0; q &lt; 3; q++) {
        set&lt;char&gt; b;
        for (int i = 0; i &lt; 3; i++)
          for (int j = 0; j &lt; 3; j++) {
            int x = p * 3 + i;
            int y = q * 3 + j;
            if (board[y][x] != '.' &amp;&amp; !b.insert(board[y][x]).second)
              return false;
          }
        }
    return true;
  }
};</pre>
</div></div>



<h2><strong>Follow up:</strong></h2>



<ul><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>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-36-valid-sudoku/">花花酱 LeetCode 36. Valid Sudoku</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/hashtable/leetcode-36-valid-sudoku/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
