{"id":9547,"date":"2022-03-08T03:05:29","date_gmt":"2022-03-08T11:05:29","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9547"},"modified":"2022-03-08T03:06:32","modified_gmt":"2022-03-08T11:06:32","slug":"leetcode-2194-cells-in-a-range-on-an-excel-sheet","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-2194-cells-in-a-range-on-an-excel-sheet\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2194. Cells in a Range on an Excel Sheet"},"content":{"rendered":"\n<p>A cell&nbsp;<code>(r, c)<\/code>&nbsp;of an excel sheet is represented as a string&nbsp;<code>\"&lt;col&gt;&lt;row&gt;\"<\/code>&nbsp;where:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>&lt;col&gt;<\/code>&nbsp;denotes the column number&nbsp;<code>c<\/code>&nbsp;of the cell. It is represented by&nbsp;<strong>alphabetical letters<\/strong>.<ul><li>For example, the&nbsp;<code>1<sup>st<\/sup><\/code>&nbsp;column is denoted by&nbsp;<code>'A'<\/code>, the&nbsp;<code>2<sup>nd<\/sup><\/code>&nbsp;by&nbsp;<code>'B'<\/code>, the&nbsp;<code>3<sup>rd<\/sup><\/code>&nbsp;by&nbsp;<code>'C'<\/code>, and so on.<\/li><\/ul><\/li><li><code>&lt;row&gt;<\/code>&nbsp;is the row number&nbsp;<code>r<\/code>&nbsp;of the cell. The&nbsp;<code>r<sup>th<\/sup><\/code>&nbsp;row is represented by the&nbsp;<strong>integer<\/strong>&nbsp;<code>r<\/code>.<\/li><\/ul>\n\n\n\n<p>You are given a string&nbsp;<code>s<\/code>&nbsp;in&nbsp;the format&nbsp;<code>\"&lt;col1&gt;&lt;row1&gt;:&lt;col2&gt;&lt;row2&gt;\"<\/code>, where&nbsp;<code>&lt;col1&gt;<\/code>&nbsp;represents the column&nbsp;<code>c1<\/code>,&nbsp;<code>&lt;row1&gt;<\/code>&nbsp;represents the row&nbsp;<code>r1<\/code>,&nbsp;<code>&lt;col2&gt;<\/code>&nbsp;represents the column&nbsp;<code>c2<\/code>, and&nbsp;<code>&lt;row2&gt;<\/code>&nbsp;represents the row&nbsp;<code>r2<\/code>, such that&nbsp;<code>r1 &lt;= r2<\/code>&nbsp;and&nbsp;<code>c1 &lt;= c2<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>list of cells<\/strong><\/em>&nbsp;<code>(x, y)<\/code>&nbsp;<em>such that<\/em>&nbsp;<code>r1 &lt;= x &lt;= r2<\/code>&nbsp;<em>and<\/em>&nbsp;<code>c1 &lt;= y &lt;= c2<\/code>. The cells should be represented as&nbsp;<strong>strings<\/strong>&nbsp;in the format mentioned above and be sorted in&nbsp;<strong>non-decreasing<\/strong>&nbsp;order first by columns and then by rows.<\/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\/2022\/02\/08\/ex1drawio.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"K1:L2\"\n<strong>Output:<\/strong> [\"K1\",\"K2\",\"L1\",\"L2\"]\n<strong>Explanation:<\/strong>\nThe above diagram shows the cells which should be present in the list.\nThe red arrows denote the order in which the cells should be presented.\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\/2022\/02\/09\/exam2drawio.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"A1:F1\"\n<strong>Output:<\/strong> [\"A1\",\"B1\",\"C1\",\"D1\",\"E1\",\"F1\"]\n<strong>Explanation:<\/strong>\nThe above diagram shows the cells which should be present in the list.\nThe red arrow denotes the order in which the cells should be presented.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>s.length == 5<\/code><\/li><li><code>'A' &lt;= s[0] &lt;= s[3] &lt;= 'Z'<\/code><\/li><li><code>'1' &lt;= s[1] &lt;= s[4] &lt;= '9'<\/code><\/li><li><code>s<\/code>&nbsp;consists of uppercase English letters, digits and&nbsp;<code>':'<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>Time complexity: O((row2 &#8211; row1 + 1) * (col2 &#8211; col1 + 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  vector<string> cellsInRange(string s) {\n    vector<string> ans;\n    for (char c = s[0]; c <= s[3]; ++c)\n      for (char r = s[1]; r <= s[4]; ++r)        \n        ans.push_back(string{c, r});\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A cell&nbsp;(r, c)&nbsp;of an excel sheet is represented as a string&nbsp;&#8220;&lt;col&gt;&lt;row&gt;&#8221;&nbsp;where: &lt;col&gt;&nbsp;denotes the column number&nbsp;c&nbsp;of the cell. It is represented by&nbsp;alphabetical letters. For example, the&nbsp;1st&nbsp;column&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[222,4,770],"class_list":["post-9547","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-string","tag-table","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9547","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=9547"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9547\/revisions"}],"predecessor-version":[{"id":9549,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9547\/revisions\/9549"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}