{"id":2022,"date":"2016-05-15T10:11:29","date_gmt":"2016-05-15T02:11:29","guid":{"rendered":"http:\/\/boweihe.me\/?p=2022"},"modified":"2016-05-15T10:11:29","modified_gmt":"2016-05-15T02:11:29","slug":"leetcode-38-count-and-say","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2022","title":{"rendered":"LeetCode 38. Count and Say"},"content":{"rendered":"<h3>\u9898\u76ee<\/h3>\n<p>The count-and-say sequence is the sequence of integers beginning as follows:<br \/>\n<code>1, 11, 21, 1211, 111221, ...<\/code><br \/>\n<code>1<\/code> is read off as <code>\"one 1\"<\/code> or <code>11<\/code>.<br \/>\n<code>11<\/code> is read off as <code>\"two 1s\"<\/code> or <code>21<\/code>.<br \/>\n<code>21<\/code> is read off as <code>\"one 2<\/code>, then <code>one 1\"<\/code> or <code>1211<\/code>.<br \/>\nGiven an integer <i>n<\/i>, generate the <i>n<\/i><sup>th<\/sup> sequence.<br \/>\nNote: The sequence of integers will be represented as a string.<\/p>\n<h3>\u601d\u8def<\/h3>\n<p>\u597d\u51e0\u9898\u6ca1\u5237leetcode\u624b\u75d2\u75d2\uff0c\u53c8\u6ca1\u592a\u591a\u65f6\u95f4\uff0c\u5c31\u9009\u4e86\u4e2aEasy\u7684\u9898\u5566&#8230;\u8fd9\u4e2a\u9898\u76ee\u786e\u5b9e\u633a\u7b80\u5355\u7684\uff0c\u7edf\u8ba1\u4e00\u4e0b\u524d\u9762\u662f\u4e0d\u662f\u91cd\u590d\u5c31\u884c\u4e86\u3002\u5728\u53e5\u672b\u52a0\u4e00\u4e2a\u7279\u6b8a\u7b26\u53f7\u53ef\u4ee5\u907f\u514d\u591a\u5199\u51e0\u884c\u4ee3\u7801\u7684\u6837\u5b50~<\/p>\n<h3>\u4ee3\u7801<\/h3>\n<pre class=\"lang:c++ decode:true \">class Solution {\npublic:\n    string countAndSay(int n) {\n        string nStr = \"1$\";\n        string nextStr = \"\";\n        while(n &gt; 1){\n            char lastChr = '$';\n            int lastCount = 0;\n            for(int i=0; i&lt;nStr.size(); i++){\n                if(nStr[i] == lastChr){\n                    lastCount++;\n                } else {\n                    if(lastCount &gt; 0){\n                        string tempStr;\n                        while(lastCount &gt; 0){\n                            tempStr += (lastCount%10 +'0');\n                            lastCount \/= 10;\n                        }\n                        reverse(tempStr.begin(), tempStr.end());\n                        nextStr += tempStr;\n                        nextStr += lastChr;\n                    }\n                    lastCount = 1;\n                    lastChr = nStr[i];\n                }\n            }\n            nStr = nextStr + \"$\";\n            nextStr = \"\";\n            n--;\n        }\n        return nStr.substr(0, nStr.size()-1);\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, &#8230; 1 is read off as &#8220;one 1&#8221; or 11. 11 is read off as &#8220;two 1s&#8221; or 21. 21 is read off as &#8220;one 2, then one 1&#8221; or 1211. Given an integer n, generate the nth [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66,151],"class_list":["post-2022","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj","tag-151"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2022","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2022"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2022\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2022"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2022"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2022"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}