{"id":3220,"date":"2018-07-19T01:05:44","date_gmt":"2018-07-19T08:05:44","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3220"},"modified":"2018-07-19T01:06:05","modified_gmt":"2018-07-19T08:06:05","slug":"leetcode-345-reverse-vowels-of-a-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/two-pointers\/leetcode-345-reverse-vowels-of-a-string\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 345. Reverse Vowels of a String"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Write a function that takes a string as input and reverse only the vowels of a string.<\/p>\n<p><b>Example 1:<\/b><br \/>\nGiven s = &#8220;hello&#8221;, return &#8220;holle&#8221;.<\/p>\n<p><b>Example 2:<\/b><br \/>\nGiven s = &#8220;leetcode&#8221;, return &#8220;leotcede&#8221;.<\/p>\n<p><b>Note:<\/b><br \/>\nThe vowels does not include the letter &#8220;y&#8221;.<\/p>\n<h1><strong>Solution<\/strong><\/h1>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 8 ms\r\nclass Solution {\r\npublic:\r\n    string reverseVowels(string s) {\r\n      int i = 0;\r\n      int j = s.size() - 1;\r\n      while (i &lt; j) {\r\n        while (!isVowel(s[i]) &amp;&amp; i &lt; j) ++i;\r\n        while (!isVowel(s[j]) &amp;&amp; i &lt; j) --j;                \r\n        swap(s[i++], s[j--]);\r\n      }\r\n      return s;\r\n    }\r\nprivate:\r\n  bool isVowel(char c) {\r\n    c = tolower(c);\r\n    return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Given s = &#8220;hello&#8221;, return&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[176],"tags":[222,246,4,175],"class_list":["post-3220","post","type-post","status-publish","format-standard","hentry","category-two-pointers","tag-easy","tag-reverse","tag-string","tag-two-pointers","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3220","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=3220"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3220\/revisions"}],"predecessor-version":[{"id":3222,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3220\/revisions\/3222"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}