{"id":2746,"date":"2020-01-12T10:40:47","date_gmt":"2020-01-12T02:40:47","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=2746"},"modified":"2020-01-12T10:40:47","modified_gmt":"2020-01-12T02:40:47","slug":"%e3%80%90leetcode%e3%80%910006-z-%e5%ad%97%e5%bd%a2%e5%8f%98%e6%8d%a2","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/algorithm\/2746\/","title":{"rendered":"\u3010LeetCode\u30110006 Z \u5b57\u5f62\u53d8\u6362"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/zigzag-conversion\">https:\/\/leetcode-cn.com\/problems\/zigzag-conversion<\/a><\/p>\n\n\n<p>\u4ee3\u7801\uff1a<\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-<br \/><br \/>class Solution(object):<br \/>    def convert(self, s, numRows):<br \/>        <em>\"\"\"<br \/><\/em><em>        <\/em><strong><em>:type<\/em><\/strong><em> s: str<br \/><\/em><em>        <\/em><strong><em>:type<\/em><\/strong><em> numRows: int<br \/><\/em><em>        <\/em><strong><em>:rtype<\/em><\/strong><em>: str<br \/><\/em><em>        \"\"\"<br \/><\/em><em>        <\/em>if numRows == 1 or numRows == len(s) or len(s) &lt; numRows:<br \/>            return s<br \/>        s_len = len(s)<br \/>        pre_jump_len = 2 * numRows<br \/>        next_jump_len = -2<br \/>        current_idx = 0<br \/>        latest_idx = 0<br \/>        result = ''<br \/>        for i in range(numRows):<br \/>            if pre_jump_len &gt; 2:<br \/>                pre_jump_len -= 2<br \/>            else:<br \/>                pre_jump_len = 2 * numRows - 2<br \/>            if next_jump_len &lt; 2 * numRows-2:<br \/>                next_jump_len += 2<br \/>            else:<br \/>                next_jump_len = 0<br \/>            result = result + s[current_idx]<br \/>            latest_idx = current_idx<br \/>            for j in range(s_len):<br \/>                if current_idx + pre_jump_len &lt; s_len:<br \/>                    result = result + s[current_idx + pre_jump_len]<br \/>                    current_idx = current_idx + pre_jump_len<br \/>                else:<br \/>                    current_idx = latest_idx + 1<br \/>                    break<br \/>                if next_jump_len == 0:<br \/>                    continue<br \/>                if current_idx + next_jump_len &lt; s_len:<br \/>                    result = result + s[current_idx + next_jump_len]<br \/>                    current_idx = current_idx + next_jump_len<br \/>                else:<br \/>                    current_idx = latest_idx + 1<br \/>                    break<br \/>        return result<br \/>        pass<br \/><br \/><br \/>if __name__ == '__main__':<br \/>    s = 'PAYPALISHIRING'<br \/>    print(Solution().convert(s, 5))<br \/><br \/><\/pre>\n\n\n<p><strong>\u601d\u8def<\/strong>\uff1a\u5982\u4e0b\u56fe\u53ef\u4ee5\u8f7b\u677e\u53d1\u73b0Z\u5b57\u6296\u52a8\u89c4\u5f8b\uff0c\u9996\u884c\u95f4\u96942*numRows-2\u4e2a\u5143\u7d20\uff0c\u6b21\u884c\u95f4\u96942*numRows-4\u4e2a\u5143\u7d20\u3002\u4f46\u662f\u8981\u6ce8\u610f\uff0c\u9664\u4e86\u9996\u4f4d\u884c\u5747\u6709\u4e24\u4e2d\u8df3\u8fc7\u957f\u5ea6\uff08\u4f8b\u5982\u7b2c\u4e8c\u884cE\u8df34\u4e2a\u5230O\uff0cO\u67612\u4e2a\u5230E\uff0cE\u518d\u8df34\u4e2a\u5230I\uff0cI\u8df32\u4e2a\u5230I\uff09\uff0c\u4ed6\u4eec\u7684\u548c\u4e3a2*numRows\u3002<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"367\" height=\"220\" src=\"\/wp-content\/uploads\/2020\/01\/\u56fe\u7247-37.png\" alt=\"\" class=\"wp-image-2747\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/01\/\u56fe\u7247-37.png 367w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/01\/\u56fe\u7247-37-300x180.png 300w\" sizes=\"(max-width: 367px) 100vw, 367px\" \/><\/figure><\/div>\n\n\n<p>\u4f9d\u7167\u8fd9\u4e2a\u601d\u8def\uff0c\u91c7\u7528\u4e24\u4e2a\u8df3\u8fc7\u957f\u5ea6\u6807\u8bb0\uff0c\u4e00\u4e2a\u4ee3\u8868\u7b2c\u4e00\u6b21\u8df3\u7684\u957f\u5ea6\uff0c\u4e00\u4e2a\u4ee3\u8868\u7b2c\u4e8c\u6b21\u8df3\u7684\u957f\u5ea6\u3002\u56e0\u6b64\u53ea\u6709\u6700\u540e\u4e00\u884c\u6570\u636e\u9700\u8981\u7279\u6b8a\u5904\u7406\uff0c\u53ea\u8981\u5f53\u9996\u6b21\u8df3\u8fc7\u957f\u5ea6\u7f29\u77ed\u4e3a2\u65f6\u91cd\u65b0\u521d\u59cb\u5316\u957f\u5ea6\u52302*numRows-2\u5373\u53ef\u3002<\/p>\n\n\n<p>\u540c\u65f6\u6ce8\u610f\u7279\u6b8a\u6570\u636e\uff0c\u4f8b\u5982s\u7684\u957f\u5ea6\u6bd4numRows\u5c0f\u7b49\u7279\u6b8a\u60c5\u51b5\u7684\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode-cn.com\/problems\/zigzag-conversion [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10],"tags":[],"views":1624,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2746"}],"collection":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2746"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2746\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2746"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2746"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2746"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}