{"id":9421,"date":"2022-01-16T06:09:12","date_gmt":"2022-01-16T14:09:12","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9421"},"modified":"2022-01-16T06:20:10","modified_gmt":"2022-01-16T14:20:10","slug":"leetcode-2133-check-if-every-row-and-column-contains-all-numbers","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-2133-check-if-every-row-and-column-contains-all-numbers\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2133. Check if Every Row and Column Contains All Numbers"},"content":{"rendered":"\n<p>An&nbsp;<code>n x n<\/code>&nbsp;matrix is&nbsp;<strong>valid<\/strong>&nbsp;if every row and every column contains&nbsp;<strong>all<\/strong>&nbsp;the integers from&nbsp;<code>1<\/code>&nbsp;to&nbsp;<code>n<\/code>&nbsp;(<strong>inclusive<\/strong>).<\/p>\n\n\n\n<p>Given an&nbsp;<code>n x n<\/code>&nbsp;integer matrix&nbsp;<code>matrix<\/code>, return&nbsp;<code>true<\/code>&nbsp;<em>if the matrix is&nbsp;<strong>valid<\/strong>.<\/em>&nbsp;Otherwise, return&nbsp;<code>false<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/12\/21\/example1drawio.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> matrix = [[1,2,3],[3,1,2],[2,3,1]]\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> In this case, n = 3, and every row and column contains the numbers 1, 2, and 3.\nHence, we return true.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/12\/21\/example2drawio.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> matrix = [[1,1,1],[1,2,3],[1,2,3]]\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> In this case, n = 3, but the first row and the first column do not contain the numbers 2 or 3.\nHence, we return false.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == matrix.length == matrix[i].length<\/code><\/li><li><code>1 &lt;= n &lt;= 100<\/code><\/li><li><code>1 &lt;= matrix[i][j] &lt;= n<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Bitset \/ hashtable<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n<sup>2<\/sup>)<br>Space complexity: O(n)<\/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 checkValid(vector<vector<int>>& matrix) {\n    const int n = matrix.size();\n    for (int i = 0; i < n; ++i) {\n      bitset<101> row;\n      bitset<101> col;\n      for (int j = 0; j < n; ++j)\n        col[matrix[i][j]] = row[matrix[j][i]] = true;      \n      if (row.count() != n || col.count() != n) \n        return false;\n    }\n    return true;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>An&nbsp;n x n&nbsp;matrix is&nbsp;valid&nbsp;if every row and every column contains&nbsp;all&nbsp;the integers from&nbsp;1&nbsp;to&nbsp;n&nbsp;(inclusive). Given an&nbsp;n x n&nbsp;integer matrix&nbsp;matrix, return&nbsp;true&nbsp;if the matrix is&nbsp;valid.&nbsp;Otherwise, return&nbsp;false. Example 1: Input:&#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":[232,222],"class_list":["post-9421","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-bitset","tag-easy","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9421","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=9421"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9421\/revisions"}],"predecessor-version":[{"id":9423,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9421\/revisions\/9423"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}