{"id":5078,"date":"2019-04-17T23:00:15","date_gmt":"2019-04-18T06:00:15","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5078"},"modified":"2019-04-17T23:01:05","modified_gmt":"2019-04-18T06:01:05","slug":"leetcode-38-count-and-say","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-38-count-and-say\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 38. Count and Say"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>Problem<\/strong><\/h2>\n\n\n\n<p><a href=\"https:\/\/leetcode.com\/problems\/count-and-say\/\">https:\/\/leetcode.com\/problems\/count-and-say\/<\/a> <\/p>\n\n\n\n<p>The count-and-say sequence is the sequence of integers with the first five terms as following:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted; crayon:false\">1.     1\n2.     11\n3.     21\n4.     1211\n5.     111221\n<\/pre>\n\n\n\n<p><code>1<\/code>&nbsp;is read off as&nbsp;<code>\"one 1\"<\/code>&nbsp;or&nbsp;<code>11<\/code>.<br><code>11<\/code>&nbsp;is read off as&nbsp;<code>\"two 1s\"<\/code>&nbsp;or&nbsp;<code>21<\/code>.<br><code>21<\/code>&nbsp;is read off as&nbsp;<code>\"one 2<\/code>, then&nbsp;<code>one 1\"<\/code>&nbsp;or&nbsp;<code>1211<\/code>.<\/p>\n\n\n\n<p>Given an integer&nbsp;<em>n<\/em>&nbsp;where 1 \u2264&nbsp;<em>n<\/em>&nbsp;\u2264 30, generate the&nbsp;<em>n<\/em><sup>th<\/sup>&nbsp;term of the count-and-say sequence.<\/p>\n\n\n\n<p>Note: Each term of the sequence of integers will be represented as a string.<\/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> 1\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> 4\n<strong>Output:<\/strong> \"1211\"<\/pre>\n\n\n\n<p><strong>Solution: Recursion + Simulation<\/strong><\/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, 4 ms, 8.6 MB\nclass Solution {\npublic:\n  string countAndSay(int n) {\n    string ans = \"1\";\n    for (int i = 1; i < n; ++i)\n      ans = say(ans);\n    return ans;\n  }\nprivate:\n  string say(const string&#038; n){\n    string ans;\n    int s = 0, l = n.length();\n    for (int e = 1; e <= l; ++e)\n      if (e == l || n[s] != n[e]) {\n        int count = e - s;\n        ans += '0' + count;\n        ans += n[s];\n        s = e;\n      }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem https:\/\/leetcode.com\/problems\/count-and-say\/ The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 2. 11 3. 21 4. 1211&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[222,17,179,4],"class_list":["post-5078","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-easy","tag-recursion","tag-simulation","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5078","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=5078"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5078\/revisions"}],"predecessor-version":[{"id":5081,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5078\/revisions\/5081"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5078"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5078"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5078"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}