{"id":8884,"date":"2021-11-28T14:35:07","date_gmt":"2021-11-28T22:35:07","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8884"},"modified":"2021-11-28T19:46:04","modified_gmt":"2021-11-29T03:46:04","slug":"leetcode-171-excel-sheet-column-number","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-171-excel-sheet-column-number\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 171. Excel Sheet Column Number"},"content":{"rendered":"\n<p>Given a string&nbsp;<code>columnTitle<\/code>&nbsp;that represents the column title as appear in an Excel sheet, return&nbsp;<em>its corresponding column number<\/em>.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\">A -> 1\nB -> 2\nC -> 3\n...\nZ -> 26\nAA -> 27\nAB -> 28 \n...\n<\/pre>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> columnTitle = \"A\"\n<strong>Output:<\/strong> 1\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> columnTitle = \"AB\"\n<strong>Output:<\/strong> 28\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> columnTitle = \"ZY\"\n<strong>Output:<\/strong> 701\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> columnTitle = \"FXSHRXW\"\n<strong>Output:<\/strong> 2147483647\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= columnTitle.length &lt;= 7<\/code><\/li><li><code>columnTitle<\/code>&nbsp;consists only of uppercase English letters.<\/li><li><code>columnTitle<\/code>&nbsp;is in the range&nbsp;<code>[\"A\", \"FXSHRXW\"]<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Base conversion<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<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++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  int titleToNumber(string s) {\n    int ans = 0;    \n    for (int i = s.length() - 1, cur = 0; i &gt;= 0; --i) {\n      cur = cur ? cur * 26 : 1;\n      ans += (s[i] - 'A' + 1) * cur;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Related Problems<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-168-excel-sheet-column-title\/\" data-type=\"post\">\u82b1\u82b1\u9171 LeetCode 168. Excel Sheet Column Title<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;columnTitle&nbsp;that represents the column title as appear in an Excel sheet, return&nbsp;its corresponding column number. For example: A -> 1 B -> 2&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[93,222,31],"class_list":["post-8884","post","type-post","status-publish","format-standard","hentry","category-math","tag-conversion","tag-easy","tag-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8884","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=8884"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8884\/revisions"}],"predecessor-version":[{"id":8898,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8884\/revisions\/8898"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}