{"id":2282,"date":"2018-03-21T22:10:55","date_gmt":"2018-03-22T05:10:55","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2282"},"modified":"2018-03-21T22:11:33","modified_gmt":"2018-03-22T05:11:33","slug":"leetcode-541-reverse-string-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-541-reverse-string-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 541. Reverse String II"},"content":{"rendered":"<div class=\"question-description\">\n<h1><strong>Problem<\/strong><\/h1>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u628a\u5b57\u7b26\u4e32\u5206\u6210\u5757\uff0c\u6bcf\u5757\u957f\u5ea62k\uff0c\u5355\u72ec\u53cd\u8f6c\u6bcf\u4e00\u5757\u7684\u524dk\u7684\u5b57\u7b26\u3002<\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/reverse-string-ii\/description\/\">https:\/\/leetcode.com\/problems\/reverse-string-ii\/description\/<\/a><\/p>\n<div>\n<p>Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and left the other as original.<b>Example:<\/b><\/p>\n<pre class=\"crayon:false \"><b>Input:<\/b> s = \"abcdefg\", k = 2\r\n<b>Output:<\/b> \"bacdfeg\"\r\n<\/pre>\n<p><b>Restrictions:<\/b><\/p>\n<ol>\n<li>The string consists of lower English letters only.<\/li>\n<li>Length of the given string and k will in the range [1, 10000]<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<h1><strong>Solution<\/strong><\/h1>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 9 ms\r\nclass Solution {\r\npublic:\r\n  string reverseStr(string s, int k) {\r\n    const int n = s.length();\r\n    for (int i = 0; i &lt;= n \/ k; i += 2)      \r\n      std::reverse(s.begin() + i * k, s.begin() + min(n, (i + 1) * k));\r\n    return s;\r\n  }\r\n};<\/pre>\n<h1><strong>Related Problems<\/strong><\/h1>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-557-reverse-words-in-a-string-iii\/\">\u82b1\u82b1\u9171 LeetCode 557 Reverse Words in a String III<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem \u9898\u76ee\u5927\u610f\uff1a\u628a\u5b57\u7b26\u4e32\u5206\u6210\u5757\uff0c\u6bcf\u5757\u957f\u5ea62k\uff0c\u5355\u72ec\u53cd\u8f6c\u6bcf\u4e00\u5757\u7684\u524dk\u7684\u5b57\u7b26\u3002 https:\/\/leetcode.com\/problems\/reverse-string-ii\/description\/ Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the&#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":[222,246,4],"class_list":["post-2282","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-reverse","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2282","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=2282"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2282\/revisions"}],"predecessor-version":[{"id":2284,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2282\/revisions\/2284"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}