{"id":1925,"date":"2018-03-02T23:43:42","date_gmt":"2018-03-03T07:43:42","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1925"},"modified":"2018-03-02T23:53:11","modified_gmt":"2018-03-03T07:53:11","slug":"leetcode-557-reverse-words-in-a-string-iii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-557-reverse-words-in-a-string-iii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 557 Reverse Words in a String III"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u72ec\u7acb\u53cd\u8f6c\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5355\u8bcd\u3002<\/p>\n<p>Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input: \"Let's take LeetCode contest\"\r\nOutput: \"s'teL ekat edoCteeL tsetnoc\"\r\n<\/pre>\n<p><b>Note:<\/b>\u00a0In the string, each word is separated by single space and there will not be any extra space in the string.<\/p>\n<p><strong>Idea: Brute Force<\/strong><\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 23 ms\r\nclass Solution {\r\npublic:\r\n  string reverseWords(string s) {    \r\n    int index = 0;  \r\n    for (int i = 0; i &lt;= s.length(); ++i) {\r\n      if (i == s.length() || s[i] == ' ') {\r\n        std::reverse(s.begin() + index, s.begin() + i);\r\n        index = i + 1;\r\n      }\r\n    }\r\n    return s;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>Python3<\/p>\n<pre class=\"lang:python decode:true \">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 44 ms (beats 100%)\r\n\"\"\"\r\nclass Solution:\r\n  def reverseWords(self, s):        \r\n    return ' '.join(map(lambda x: x[::-1], s.split(' ')))<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u72ec\u7acb\u53cd\u8f6c\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5355\u8bcd\u3002 Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word&#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":[227,168],"class_list":["post-1925","post","type-post","status-publish","format-standard","hentry","category-string","tag-reversed","tag-words","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1925","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=1925"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1925\/revisions"}],"predecessor-version":[{"id":1929,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1925\/revisions\/1929"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}