{"id":8866,"date":"2021-11-28T13:45:14","date_gmt":"2021-11-28T21:45:14","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8866"},"modified":"2021-11-28T13:53:35","modified_gmt":"2021-11-28T21:53:35","slug":"leetcode-151-reverse-words-in-a-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-151-reverse-words-in-a-string\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 151. Reverse Words in a String"},"content":{"rendered":"\n<p>Given an input string&nbsp;<code>s<\/code>, reverse the order of the&nbsp;<strong>words<\/strong>.<\/p>\n\n\n\n<p>A&nbsp;<strong>word<\/strong>&nbsp;is defined as a sequence of non-space characters. The&nbsp;<strong>words<\/strong>&nbsp;in&nbsp;<code>s<\/code>&nbsp;will be separated by at least one space.<\/p>\n\n\n\n<p>Return&nbsp;<em>a string of the words in reverse order concatenated by a single space.<\/em><\/p>\n\n\n\n<p><strong>Note<\/strong>&nbsp;that&nbsp;<code>s<\/code>&nbsp;may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.<\/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> s = \"the sky is blue\"\n<strong>Output:<\/strong> \"blue is sky the\"\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> s = \"  hello world  \"\n<strong>Output:<\/strong> \"world hello\"\n<strong>Explanation:<\/strong> Your reversed string should not contain leading or trailing spaces.\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> s = \"a good   example\"\n<strong>Output:<\/strong> \"example good a\"\n<strong>Explanation:<\/strong> You need to reduce multiple spaces between two words to a single space in the reversed string.\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> s = \"  Bob    Loves  Alice   \"\n<strong>Output:<\/strong> \"Alice Loves Bob\"\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"Alice does not even like bob\"\n<strong>Output:<\/strong> \"bob like even not does Alice\"\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= s.length &lt;= 10<sup>4<\/sup><\/code><\/li><li><code>s<\/code>&nbsp;contains English letters (upper-case and lower-case), digits, and spaces&nbsp;<code>' '<\/code>.<\/li><li>There is&nbsp;<strong>at least one<\/strong>&nbsp;word in&nbsp;<code>s<\/code>.<\/li><\/ul>\n\n\n\n<p><strong>Follow-up:&nbsp;<\/strong>If the string data type is mutable in your language, can&nbsp;you solve it&nbsp;<strong>in-place<\/strong>&nbsp;with&nbsp;<code>O(1)<\/code>&nbsp;extra space?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Stack<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/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\nclass Solution {\npublic:\n  string reverseWords(string s) {\n    stringstream ss(s);\n    stack<string> st;\n    string w;\n    while (ss >> w)\n      st.push(w);\n    string ans;\n    while (!st.empty()) {\n      ans += st.top();\n      st.pop();\n      if (!st.empty()) ans += ' ';\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: In-Place<\/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++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  string reverseWords(string& s) {\n    reverse(begin(s), end(s));\n    int l = 0;\n    for (int i = 0, j = 0; i < s.length(); ++i) {\n      if (s[i] == ' ') continue;\n      if (l) ++l;\n      j = i;\n      while (j < s.length() &#038;&#038; s[j] != ' ') ++j;\n      reverse(begin(s) + l, begin(s) + j);\n      l += (j - i);\n      i = j;\n    }\n    s.resize(l);\n    return s;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an input string&nbsp;s, reverse the order of the&nbsp;words. A&nbsp;word&nbsp;is defined as a sequence of non-space characters. The&nbsp;words&nbsp;in&nbsp;s&nbsp;will be separated by at least one space.&#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":[177,246,4],"class_list":["post-8866","post","type-post","status-publish","format-standard","hentry","category-string","tag-medium","tag-reverse","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8866","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=8866"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8866\/revisions"}],"predecessor-version":[{"id":8869,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8866\/revisions\/8869"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}