{"id":2004,"date":"2016-04-22T13:39:52","date_gmt":"2016-04-22T05:39:52","guid":{"rendered":"http:\/\/boweihe.me\/?p=2004"},"modified":"2016-04-22T13:39:52","modified_gmt":"2016-04-22T05:39:52","slug":"leetcode-151-reverse-words-in-a-string","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2004","title":{"rendered":"LeetCode 151. Reverse Words in a String"},"content":{"rendered":"<h3>\u9898\u76ee<\/h3>\n<p>Given an input string, reverse the string word by word.<br \/>\nFor example,<br \/>\nGiven s = &#8220;<code>the sky is blue<\/code>&#8220;,<br \/>\nreturn &#8220;<code>blue is sky the<\/code>&#8220;.<br \/>\n<b><span style=\"color: red;\">Update (2015-02-12):<\/span><\/b><br \/>\nFor C programmers: Try to solve it <i>in-place<\/i> in <i>O<\/i>(1) space.<\/p>\n<h3>\u601d\u8def<\/h3>\n<p>\u4e00\uff01\u5b9a\uff01\u8981\uff01\u8003\uff01\u8651\uff01\u5404\uff01\u79cd\uff01\u60c5\uff01\u51b5\uff01\u6bd4\u5982<\/p>\n<pre class=\"lang:c# decode:true \">\" \"\/\/\u4e00\u4e2a\u7a7a\u683c\n\"   What am   I doing     \"<\/pre>\n<p>\u7136\u540e\u8fd9\u79cd\u5012\u8f6c\u95ee\u9898\u7684\u539f\u5730\u5904\u7406\u601d\u8def\u5e94\u8be5\u90fd\u660e\u767d\u7684\uff0c\u5148\u628a\u6574\u4e2a\u5b57\u7b26\u4e32\u5012\u8f6c\u4e00\u904d\uff0c\u7136\u540e\u6309\u7167\u5355\u8bcd\u518d\u5012\u8f6c\u56de\u6765\uff0c\u62ff&#8221;the sky is blue&#8221;\u4e3a\u4f8b\uff1a<\/p>\n<ol>\n<li>&#8220;<span style=\"color: #ff6600;\">eulb si yks eht<\/span>&#8220;<\/li>\n<li>&#8220;<span style=\"color: #ff6600;\">blue<\/span> si yks eht&#8221;<\/li>\n<li>&#8220;blue <span style=\"color: #ff6600;\">is<\/span> yks eht&#8221;<\/li>\n<li>&#8230;<\/li>\n<li>&#8220;blue is sky the&#8221;<\/li>\n<\/ol>\n<h3>\u4ee3\u7801<\/h3>\n<pre class=\"lang:c++ decode:true \">class Solution {\npublic:\n\tvoid reverseWordsSub(string &amp;s, int start, int end) {\n\t\t\/\/[start,end]\n\t\tchar c;\n\t\tint count = 0;\n\t\tconst int maxInd = (end - start - 1) \/ 2 + start;\n\t\tfor (int i = start; i &lt;= maxInd; i++) {\n\t\t\tc = s[i];\n\t\t\ts[i] = s[end - count];\n\t\t\ts[end - count] = c;\n\t\t\tcount++;\n\t\t}\n\t}\n\tvoid clearStr(string &amp;s) {\n\t\t\/\/Clear unnecessary spaces\n\t\tint validIndex = -1;\n\t\tbool spaceFlag = false;\n\t\tfor (int i = 0; i&lt;s.size(); i++) {\n\t\t\tif (s[i] != ' ') {\n\t\t\t\ts[++validIndex] = s[i];\n\t\t\t\tspaceFlag = false;\n\t\t\t}\n\t\t\telse if (!spaceFlag) {\n\t\t\t\tif (validIndex &lt; 0) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tspaceFlag = true;\n\t\t\t\ts[++validIndex] = s[i];\n\t\t\t}\n\t\t}\n\t\tif (spaceFlag)\n\t\t\tvalidIndex--;\n\t\tif (validIndex &lt; 0)\n\t\t\ts = \"\";\n\t\telse\n\t\t\ts = s.substr(0, validIndex + 1);\n\t}\n\tvoid reverseWords(string &amp;s) {\n\t\tclearStr(s);\n\t\tint strSz = s.size();\n\t\treverseWordsSub(s, 0, strSz - 1);\n\t\tint start = 0;\n\t\tfor (int i = 0; i&lt;strSz; i++) {\n\t\t\tif (s[i] == ' ') {\n\t\t\t\treverseWordsSub(s, start, i - 1);\n\t\t\t\tstart = i + 1;\n\t\t\t}\n\t\t}\n\t\treverseWordsSub(s, start, strSz - 1);\n\t}\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee Given an input string, reverse the string word by word. For example, Given s = &#8220;the sky is blue&#8220;, return &#8220;blue is sky the&#8220;. Update (2015-02-12): For C programmers: Try to solve it in-place in O(1) space. \u601d\u8def \u4e00\uff01\u5b9a\uff01\u8981\uff01\u8003\uff01\u8651\uff01\u5404\uff01\u79cd\uff01\u60c5\uff01\u51b5\uff01\u6bd4\u5982 &#8221; &#8220;\/\/\u4e00\u4e2a\u7a7a\u683c &#8221; What am I doing &#8221; \u7136\u540e\u8fd9\u79cd\u5012\u8f6c\u95ee\u9898\u7684\u539f\u5730\u5904\u7406\u601d\u8def\u5e94\u8be5\u90fd\u660e\u767d\u7684\uff0c\u5148\u628a\u6574\u4e2a\u5b57\u7b26\u4e32\u5012\u8f6c\u4e00\u904d\uff0c\u7136\u540e\u6309\u7167\u5355\u8bcd\u518d\u5012\u8f6c\u56de\u6765\uff0c\u62ff&#8221;the sky is blue&#8221;\u4e3a\u4f8b\uff1a &#8220;eulb si yks [&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-2004","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\/2004","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=2004"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2004\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2004"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}