{"id":477,"date":"2021-09-11T13:40:17","date_gmt":"2021-09-11T05:40:17","guid":{"rendered":"https:\/\/blog.cauchyschwarz.com\/?p=477"},"modified":"2024-07-24T23:12:24","modified_gmt":"2024-07-24T15:12:24","slug":"%e7%9f%a9%e9%98%b5%e6%97%8b%e8%bd%ac","status":"publish","type":"post","link":"https:\/\/blog.cauchyschwarz.com\/?p=477","title":{"rendered":"\u77e9\u9635\u65cb\u8f6c"},"content":{"rendered":"\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-right counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69e3466b381d1\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69e3466b381d1\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=477\/#nn%E7%9F%A9%E9%98%B5%E6%97%8B%E8%BD%AC\" >n*n\u77e9\u9635\u65cb\u8f6c<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=477\/#%E9%A2%98%E7%9B%AE%E6%8F%8F%E8%BF%B0\" >\u9898\u76ee\u63cf\u8ff0<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=477\/#%E9%A2%98%E8%A7%A3\" >\u9898\u89e3<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=477\/#%E6%96%B9%E6%B3%95%E4%B8%80\" >\u65b9\u6cd5\u4e00<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=477\/#%E6%96%B9%E6%B3%95%E4%BA%8C\" >\u65b9\u6cd5\u4e8c<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=477\/#%E5%BB%B6%E4%BC%B81_%E6%95%B0%E7%BB%84%E6%97%8B%E8%BD%AC\" >\u5ef6\u4f381 \u6570\u7ec4\u65cb\u8f6c<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=477\/#%E6%80%9D%E8%B7%AF%E4%B8%80\" >\u601d\u8def\u4e00<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=477\/#%E6%80%9D%E8%B7%AF%E4%BA%8C\" >\u601d\u8def\u4e8c<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=477\/#%E6%80%9D%E8%B7%AF%E4%B8%89\" >\u601d\u8def\u4e09<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=477\/#%E5%BB%B6%E4%BC%B82_%E6%9B%B4%E4%B8%80%E8%88%AC%E7%9A%84%E7%9F%A9%E9%98%B5%E6%97%8B%E8%BD%AC\" >\u5ef6\u4f382 \u66f4\u4e00\u822c\u7684\u77e9\u9635\u65cb\u8f6c<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"nn%E7%9F%A9%E9%98%B5%E6%97%8B%E8%BD%AC\"><\/span>n*n\u77e9\u9635\u65cb\u8f6c<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E9%A2%98%E7%9B%AE%E6%8F%8F%E8%BF%B0\"><\/span><a href=\"https:\/\/leetcode-cn.com\/problems\/rotate-matrix-lcci\/\">\u9898\u76ee\u63cf\u8ff0<\/a><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u5bf9\u4e00\u4e2an*n\u77e9\u9635\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u987a\u65f6\u948890\u5ea6\u65cb\u8f6c\uff0c\u8981\u6c42\u4e0d\u5f97new\u65b0\u77e9\u9635\uff0c\u5fc5\u987b\u539f\u5730\u64cd\u4f5c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E9%A2%98%E8%A7%A3\"><\/span>\u9898\u89e3<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u76ee\u524d\u60f3\u5230\u7684\u6700\u81ea\u7136\u7684\u65b9\u6cd5\uff1a<\/mark><\/p>\n\n\n\n<p>\u5148\u8003\u8651\u5750\u6807(x,y)\u7684\u70b9\u7ed5\u539f\u70b9\u987a\u65f6\u9488\u8f6c90\u5ea6\u540e\u7684\u5750\u6807\uff0c\u56e0\u4e3a\u77e9\u9635\u5185\u7684\u70b9\u90fd\u53ef\u4ee5\u770b\u505a\u4ee5\u77e9\u9635\u4e2d\u5fc3\u4e3a\u539f\u70b9\u7684\u70b9\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u53ef\u4ee5\u5229\u7528\u590d\u6570\u6765\u6c42\uff0c\u7b80\u5316\u4e00\u4e0b\uff0c\u5047\u8bbe\u8fd9\u4e2a\u70b9\u662f\u5355\u4f4d\u5706\u4e0a\u7684\u70b9$e^{i\\theta} = cos\\theta + isin\\theta$\uff0c\u987a\u65f6\u9488\u65cb\u8f6c90\u5ea6\u540e\uff0c<\/p>\n\n\n\n<p>$e^{i\\theta} \\times e^{i  (-\\frac{\\pi}{2})} = sin\\theta &#8211; cos\\theta i$\uff0c\u5373\u5750\u6807\u53d8\u6210\u4e86\u53d8\u6210(y, -x)\u3002<\/p>\n\n\n\n<p>\u63a5\u7740\u518d\u8003\u8651(x,y)\u8981\u600e\u4e48\u53d8\u6210(y, -x)\uff0c\u53ef\u4ee5\u5148(x,y)->(y, x)\uff0c\u518d(y, x)->(y, -x)\uff0c\u8fd9\u4e24\u4e2a\u64cd\u4f5c\u90fd\u53ef\u4ee5\u4e0d\u7528\u7533\u8bf7\u65b0\u7684\u5185\u5b58\uff0c\u91c7\u53d6\u5148\u6309\u5bf9\u89d2\u7ebfswap\uff0c\u518d\u6309\u6c34\u5e73\u4e2d\u5fc3\u7ebfswap\u7684\u65b9\u5f0f\u5b8c\u6210\u3002<\/p>\n\n\n\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5c31\u9002\u5408\u6211\u8fd9\u79cd\u60f3\u4e0d\u6e05\u5750\u6807\u53d8\u6362\u5173\u7cfb\u7684\u4eba\uff0c\u53ea\u7ba1\u7b97\u7b97\u7b97\u5c31\u884c\u4e86\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%96%B9%E6%B3%95%E4%B8%80\"><\/span>\u65b9\u6cd5\u4e00<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/rotate-matrix-lcci\/solution\/c-tu-jie-yuan-di-cao-zuo-ji-bai-shuang-bai-vv-by-t\/\">https:\/\/leetcode-cn.com\/problems\/rotate-matrix-lcci\/solution\/c-tu-jie-yuan-di-cao-zuo-ji-bai-shuang-bai-vv-by-t\/<\/a><\/p>\n\n\n\n<p><strong>\u5148\u5206\u6790<\/strong>\u77e9\u9635\u4e2d\u7684\u5143\u7d20$a_{i,j}$(\u5047\u8bbe\u5de6\u4e0a\u89d2\u5143\u7d20\u4e3a$a_{0,0}$)\u987a\u65f6\u9488\u65cb\u8f6c\u4e4b\u540e\u5230\u4e86\u65b0\u77e9\u9635\u7684\u54ea\u4e2a\u4f4d\u7f6e\uff1a<\/p>\n\n\n\n<p>\u7531\u4e8e\u987a\u65f6\u9488\u65cb\u8f6c\u540e\uff0c$a_{i,j}$\u4f1a\u4ece\u7b2cj\u5217\u65cb\u8f6c\u5230\u7b2cj\u884c\uff0c$a_{i,j}$\u4ece\u7b2ci\u884c\u65cb\u8f6c\u5230\u5012\u6570\u7b2ci\u5217\uff0c\u6240\u4ee5\u987a\u65f6\u9488\u65cb\u8f6c90\u5ea6\u4f1a\u5c06$a_{i,j}$\u53d8\u6362\u5230$a_{j,n-1-i}$\u7684\u4f4d\u7f6e\u4e0a\u53bb\u3002<\/p>\n\n\n\n<p><strong>\u63a5\u4e0b\u6765<\/strong>\u662f\u5206\u6790\u5982\u4f55\u679a\u4e3e\u77e9\u9635\u7684\u5143\u7d20\uff0c\u8fdb\u884c\u8fd9\u79cd\u65cb\u8f6c\u64cd\u4f5c\uff0c\u8fd9\u662f\u9762\u8bd5\u65f6\u6211\u6ca1\u6709\u60f3\u51fa\u6765\u7684\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u4e00\u7684\u679a\u4e3e\u65b9\u6848\u5c31\u662f\u5c06\u6574\u4e2a\u5927\u77e9\u9635M\u5212\u5206\u4e3a\u56db\u4e2a\u5c0f\u77e9\u9635$A_{0,0}$\uff0c$A_{0,1}$\uff0c$A_{1,0}$\uff0c$A_{1,1}$\uff0c\u5bf9\u5927\u77e9\u9635M\u7684\u65cb\u8f6c90\u5ea6\u64cd\u4f5c\u5c31\u662f\u5c06$A_{0,0}$\u65cb\u8f6c\u5230\u4e86$A_{0,1}$\u7684\u4f4d\u7f6e\uff0c$A_{0,1}$\u65cb\u8f6c\u5230$A_{1,1}$\u7684\u4f4d\u7f6e\uff0c$A_{1,1}$\u65cb\u8f6c\u5230$A_{1,0}$\u7684\u4f4d\u7f6e\uff0c$A_{1,0}$\u65cb\u8f6c\u5230$A_{0,0}$\u7684\u4f4d\u7f6e\u3002\u6240\u4ee5\u9700\u8981\u505a\u7684\u5c31\u662f\u679a\u4e3e\u5de6\u4e0a\u89d2\u5c0f\u77e9\u9635$A_{0,0}$\u4e2d\u7684\u5143\u7d20$a_{i,j}$\u5373\u53ef\u3002\u5bf9\u6bcf\u4e2a$A_{0,0}$\u4e2d\u7684$a_{i,j}$(\u8fd9\u4e2a\u5750\u6807\u4ecd\u7136\u662f\u5927\u77e9\u9635M\u4e2d\u7684\u5750\u6807)\uff0c\u5c06\u5176\u65cb\u8f6c\u5230$A_{0,1}$\u4e2d\u7684$a_{j,n-1-i}$\uff0c\u7136\u540e\u5c06$a_{j,n-1-i}$\u65cb\u8f6c\u5230$A_{1,1}$\u4e2d\u7684$a_{n-1-i,n-1-j}$\uff0c\u7136\u540e\u518d\u5c06$A_{1,1}$\u4e2d\u7684$a_{n-1-i,n-1-j}$\u65cb\u8f6c\u5230$A_{1,0}$\u4e2d\u7684$a_{n-1-j,i}$\uff0c\u6700\u540e\u5c06$a_{n-1-j,i}$\u65cb\u8f6c\u5230$A_{0,0}$\u4e2d\u7684$a_{i,j}$\u3002<\/p>\n\n\n\n<p>\u5728\u7f16\u7a0b\u7684\u65f6\u5019\u5bf9$a_{i,j}$,$a_{j,n-1-i}$,$a_{n-1-i,n-1-j}$,$a_{n-1-j,i}$\u8fd9\u56db\u4e2a\u5143\u7d20\u76844\u4e2a\u65cb\u8f6c\u64cd\u4f5c\u53c8\u53ef\u4ee5\u66ff\u6362\u4e3a3\u4e2aswap\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<p>swap($a_{i,j}$,$a_{j,n-1-i}$);<\/p>\n\n\n\n<p>swap($a_{i,j}$,<meta http-equiv=\"content-type\" content=\"text\/html; charset=utf-8\">$a_{n-1-j,i}$);<\/p>\n\n\n\n<p>swap(<meta http-equiv=\"content-type\" content=\"text\/html; charset=utf-8\">$a_{n-1-i,n-1-j}$,$a_{n-1-j,i}$);<\/p>\n\n\n\n<p><strong>\u5bf9\u4e8en\u4e3a\u5947\u6570\u7684\u60c5\u51b5<\/strong>\uff0c\u5e76\u4e0d\u80fd\u50cfn\u4e3a\u5076\u6570\u90a3\u6837\u76f4\u89c2\u5730\u5212\u5206\u4e3a4\u4e2a\u5c0f\u77e9\u9635\uff0c\u800c\u662f\u53bb\u9664\u77e9\u9635\u4e2d\u5fc3\u7684\u5143\u7d20\uff0c\u5c06\u5176\u4f59\u5143\u7d20\u5212\u5206\u4e3a\u65cb\u8f6c\u65f6\u4e92\u4e0d\u76f8\u4ea4\u7684\u5c0f\u77e9\u5f62\uff0c\u6bd4\u5982\u5c06\u524d$(n-1)\/2$\u884c\u3001$(n+1)\/2$\u5217\u7684\u5143\u7d20\u4f5c\u4e3a$A_{0,0}$\u3002\u5176\u4f59\u7684$A_{0,1}$\uff0c$A_{1,0}$,$A_{1,1}$\u7684\u8303\u56f4\u53ef\u4ee5\u753b\u56fe\u7c7b\u63a8\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%96%B9%E6%B3%95%E4%BA%8C\"><\/span>\u65b9\u6cd5\u4e8c<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/rotate-matrix-lcci\/solution\/miao-dong-xi-lie-si-lu-fei-chang-jian-dan-dai-ma-y\/\">https:\/\/leetcode-cn.com\/problems\/rotate-matrix-lcci\/solution\/miao-dong-xi-lie-si-lu-fei-chang-jian-dan-dai-ma-y\/<\/a><\/p>\n\n\n\n<p>\u65b9\u6cd5\u4e00\u662f\u5c06\u6574\u4e2a\u77e9\u9635\u5212\u5206\u6210\u4e92\u4e0d\u76f8\u4ea4\u76844\u4e2a\u533a\u57df\uff0c\u679a\u4e3e\u5176\u4e2d\u4e00\u4e2a\u533a\u57df\u6765\u65cb\u8f6c\u3002<\/p>\n\n\n\n<p>\u65b9\u6cd5\u4e8c\u662f\u540c\u65f6\u5bf9\u6574\u4e2a\u77e9\u9635\u4e2d\u7684\u5143\u7d20\u5206\u6b65\u8fdb\u884c\u591a\u6b21\u64cd\u4f5c\uff0c\u6700\u7ec8\u8fbe\u5230\u5c06$a_{i,j}$\u53d8\u6362\u5230$a_{j,n-1-i}$\u7684\u6548\u679c\u3002<\/p>\n\n\n\n<p>\u5148\u5bf9\u77e9\u9635\u505a<strong>\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u7684\u7ffb\u8f6c<\/strong>(\u5176\u5b9e\u5c31\u662f\u77e9\u9635\u7684\u8f6c\u7f6e)\uff0c\u539f\u5730\u5b8c\u6210\uff0c\u8fd9\u4e2a\u65f6\u5019$a_{i,j}$\u53d8\u5230\u4e86$a_{j,i}$\uff1b\u63a5\u7740\u518d\u5bf9\u64cd\u4f5c\u540e\u7684\u77e9\u9635\u8fdb\u884c<strong>\u6839\u636e\u5217\u5e8f\u53f7\u5012\u6392<\/strong>\uff0c\u4e8e\u662f$a_{j,i}$\u53d8\u5230\u4e86$a_{j,n-1-i}$\u3002\u7efc\u5408\u4e24\u6b65\u6765\u770b\u5c31\u662f\u5c06$a_{i,j}$\u53d8\u6210\u4e86$a_{j,n-1-i}$\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%BB%B6%E4%BC%B81_%E6%95%B0%E7%BB%84%E6%97%8B%E8%BD%AC\"><\/span><a href=\"https:\/\/www.hackerrank.com\/challenges\/ctci-array-left-rotation\/problem\" data-type=\"URL\" data-id=\"https:\/\/www.hackerrank.com\/challenges\/ctci-array-left-rotation\/problem\">\u5ef6\u4f381 \u6570\u7ec4\u65cb\u8f6c<\/a><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/rotate-array\/\">https:\/\/leetcode-cn.com\/problems\/rotate-array\/<\/a><\/p>\n\n\n\n<p>\u4ee5leetcode\u8fd9\u9898\u4e3a\u4f8b\uff0cn\u4e2a\u5143\u7d20\u53f3\u79fbk\u4f4d\uff0c<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%80%9D%E8%B7%AF%E4%B8%80\"><\/span>\u601d\u8def\u4e00<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u76f4\u63a5new\u65b0\u6570\u7ec4\u6620\u5c04\uff1b$ a_{(i+k)mod \\ n} = a_{i} $<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%80%9D%E8%B7%AF%E4%BA%8C\"><\/span>\u601d\u8def\u4e8c<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u901a\u8fc7\u53f3\u79fbk\u4f4d\u8fd9\u79cd\u64cd\u4f5c\uff0c\u5b9e\u9645\u4e0a\u662f$a_{i}$\uff0c$a_{(i+k)mod\\ n}$\uff0c<meta http-equiv=\"content-type\" content=\"text\/html; charset=utf-8\">$a_{(i+2k)mod\\ n}$\uff0c<meta http-equiv=\"content-type\" content=\"text\/html; charset=utf-8\">$a_{(i+3k)mod\\ n}$\uff0c\u8fd9\u4e9b\u5143\u7d20\u5f62\u6210\u4e86\u4e00\u4e2a\u73af\uff0c\u5faa\u73af\u6362\u4e86\u4e2a\u4f4d\u7f6e\u3002\u7136\u540e\u6211\u4eec\u9700\u8981\u786e\u5b9a\u5bf9\u7ed9\u5b9a\u7684n\u548ck\uff0c\u4f1a\u5f62\u6210\u591a\u5c11\u4e2a\u8fd9\u6837\u7684\u73af\uff0c\u8fd9\u6837\u6211\u4eec\u624d\u80fd\u786e\u5b9a\u9700\u8981\u679a\u4e3e\u4ece$a_0$\u5f00\u59cb\u7684\u591a\u5c11\u4e2a\u5143\u7d20\uff0c\u7136\u540e\u5bf9\u8fd9\u4e9b\u5143\u7d20\u4f9d\u6b21\u8fdb\u884c\u5176\u6240\u5c5e\u73af\u7684\u5faa\u73af\u4f4d\u79fb\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p>\u663e\u7136\uff0c\u5982\u679c\u5b58\u5728\u591a\u4e2a\u73af\uff0c\u90a3\u4e48\u8fd9\u4e9b\u73af\u91cc\u7684\u5143\u7d20\u4e2a\u6570\u662f\u4e00\u6837\u7684\uff0c\u56e0\u6b64\u53ea\u9700\u8981\u5206\u6790$a_0$\u5143\u7d20\u6240\u5c5e\u7684\u73af\u91cc\u6709\u591a\u5c11\u4e2a\u5143\u7d20\u3002\u5982leetcode\u5b98\u65b9\u9898\u89e3\u6240\u8ff0\uff0c\u5047\u8bbe\u8fd9\u4e2a\u73af\u91cc\u6709b\u4e2a\u5143\u7d20\uff0c\u90a3\u4e48\u5c31\u6709\u516c\u5f0f\uff1a$zn = bk$\uff0c\u5373$a_0$\u5143\u7d20\u7ecf\u8fc7b\u6b21\u53f3\u79fb\u540c\u65f6\u4e5f\u904d\u5386\u6570\u7ec4z\u6b21\u540e<strong>\u7b2c\u4e00\u6b21<\/strong>\u91cd\u65b0\u56de\u5230\u4e86$a_0$\u7684\u4f4d\u7f6e\u4e0a\u3002\u8fd9\u4e2azn\u662fn\u548ck\u7684\u500d\u6570\uff0c\u540c\u65f6z\u53c8\u662f\u4e00\u4e2a\u6700\u5c0f\u7684\u4f7f\u5f97zn\u662fn\u548ck\u7684\u500d\u6570\u7684\u503c\uff0c\u56e0\u6b64zn\u662fn\u548ck\u7684\u6700\u5c0f\u516c\u500d\u6570\uff0c$zn=lcm(n,k)$\uff0c\u4e8e\u662f$b=lcm(n,k)\/k$\uff0c\u4e8e\u662f\u73af\u7684\u4e2a\u6570\u4e3a$n\/b=n\/(lcm(n,k)\/k)=nk\/lcm(n,k)=gcd(n,k)$\u3002<\/p>\n\n\n\n<p>\u56e0\u6b64\u53ea\u8981\u679a\u4e3e$a_0$\u5230$a_{gcd(n,k)-1}$\u8fd9\u4e9b\u5143\u7d20\uff0c\u5bf9\u6bcf\u4e2a\u5143\u7d20\u6240\u5728\u7684\u73af\u8fdb\u884c\u5faa\u73af\u53f3\u79fbk\u4f4d\u7684\u64cd\u4f5c\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%80%9D%E8%B7%AF%E4%B8%89\"><\/span>\u601d\u8def\u4e09<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u53f3\u79fbk\u4f4d\u8fd9\u79cd\u64cd\u4f5c\uff0c\u5b9e\u9645\u4e0a\u662f\u5c06\u524dn-k\u4e2a\u6570\u79fb\u5230\u4e86\u6700\u540e\u9762\uff0c\u5c06\u540ek\u4e2a\u6570\u79fb\u5230\u4e86\u6700\u524d\u9762\uff0c\u53ea\u662f\u4ecd\u7136\u4fdd\u6301\u539f\u6765\u7684\u987a\u5e8f\u3002\u90a3\u4e48\u5148\u5c06\u6574\u4e2a\u6570\u7ec4\u7ffb\u8f6c\uff0c\u7136\u540e\u518d\u5bf9\u524dk\u4e2a\u6570\u7ffb\u8f6c\uff0c\u540c\u65f6\u518d\u5bf9\u540en-k\u4e2a\u6570\u7ffb\u8f6c\uff0c\u6548\u679c\u4e5f\u662f\u4e00\u6837\u7684\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%BB%B6%E4%BC%B82_%E6%9B%B4%E4%B8%80%E8%88%AC%E7%9A%84%E7%9F%A9%E9%98%B5%E6%97%8B%E8%BD%AC\"><\/span><a href=\"https:\/\/www.hackerrank.com\/challenges\/matrix-rotation-algo\/problem\">\u5ef6\u4f382 \u66f4\u4e00\u822c\u7684\u77e9\u9635\u65cb\u8f6c<\/a><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u6309\u7167\u4ece\u5916\u5230\u5185\u6bcf\u4e2a\u5708\u8fdb\u884c\u65cb\u8f6c\u5c31\u597d\u4e86\uff0c\u5177\u4f53\u7684\u5bf9\u4e8e\u6bcf\u4e2a\u5708\u5148\u5c06\u5708\u5185\u7684\u5143\u7d20\u5b58\u5165vector\uff0c\u7136\u540e\u5229\u7528vector\u5bf9\u5143\u7d20\u8fdb\u884c\u65cb\u8f6c\uff0c\u518d\u5c06\u65cb\u8f6c\u540e\u7684\u5143\u7d20\u653e\u56de\u77e9\u9635\u5373\u53ef\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void shiftLeft(vector&lt;int&gt; &amp;arr, int steps) {\n    \/\/cout &lt;&lt; \"shiftLeft \" &lt;&lt; arr.size() &lt;&lt; \" \" &lt;&lt; steps &lt;&lt; endl;\n    steps = steps % arr.size();\n    vector&lt;int&gt; res(arr.size());\n    int len = arr.size();\n    for(int i = 0; i &lt; len; i++) {\n        res&#91;i] = arr&#91;(i+steps) % len];\n    }\n    arr = res;\n}\nvoid rotateCircle(vector&lt;vector&lt;int&gt;&gt; &amp;matrix, int depth, int row, int cols, int steps) {\n    \/\/cout &lt;&lt; \"rotateCircle \" &lt;&lt; depth &lt;&lt; \" \" &lt;&lt; row &lt;&lt; \" \" &lt;&lt; cols &lt;&lt; \" \" &lt;&lt; steps &lt;&lt; endl;\n    int start_r = depth;\n    int start_c = depth;\n    int len_c = cols - depth * 2;\n    int len_r = row - depth * 2;\n    vector&lt;int&gt; arr;\n    \/\/ upper edge\n    for(int i = 0; i &lt; len_c; i++) {\n        arr.push_back(matrix&#91;start_r]&#91;start_c + i]);\n    }\n    \/\/ right edge\n    for(int i = 0; i &lt; len_r - 2; i++) {\n        arr.push_back(matrix&#91;start_r + 1 + i]&#91;start_c + len_c - 1]);\n    }\n    \/\/ bottom edge\n    for(int i = len_c - 1; i &gt;= 0; i--) {\n        arr.push_back(matrix&#91;start_r + len_r - 1]&#91;start_c + i]);\n    }\n    \/\/ left edge\n    for (int i = len_r - 2; i &gt;= 1; i--) {\n        arr.push_back(matrix&#91;start_r + i]&#91;start_c]);\n    }\n    shiftLeft(arr, steps);\n    int index = 0;\n    \/\/ upper edge\n    for(int i = 0; i &lt; len_c; i++) {\n        matrix&#91;start_r]&#91;start_c + i] = arr&#91;index++];\n    }\n    \/\/ right edge\n    for(int i = 0; i &lt; len_r - 2; i++) {\n        matrix&#91;start_r + 1 + i]&#91;start_c + len_c - 1] = arr&#91;index++];\n    }\n    \/\/ bottom edge\n    for(int i = len_c - 1; i &gt;= 0; i--) {\n        matrix&#91;start_r + len_r - 1]&#91;start_c + i] = arr&#91;index++];\n    }    \n    \/\/ left edge\n    for (int i = len_r - 2; i &gt;= 1; i--) {\n        matrix&#91;start_r + i]&#91;start_c] = arr&#91;index++];\n    }    \n}\n\nvoid matrixRotation(vector&lt;vector&lt;int&gt;&gt; &amp;matrix, int r) {\n    int rows = matrix.size();\n    int cols = matrix&#91;0].size();\n    int layer = (rows &gt; cols ? cols : rows) \/ 2;\n    for(int i = 0; i &lt; layer; i++) {\n        rotateCircle(matrix, i, rows, cols, r);\n    }\n    for (int i = 0; i &lt; rows; i++) {\n        for (int j = 0; j &lt; cols; j++) {\n            cout &lt;&lt; matrix&#91;i]&#91;j] &lt;&lt; \" \";\n        }\n        cout &lt;&lt; endl;\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>n*n\u77e9\u9635\u65cb\u8f6c \u9898\u76ee\u63cf\u8ff0 \u5bf9\u4e00\u4e2an*n\u77e9\u9635\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u987a\u65f6\u948890\u5ea6\u65cb\u8f6c\uff0c\u8981\u6c42\u4e0d\u5f97new\u65b0\u77e9\u9635\uff0c\u5fc5\u987b\u539f\u5730\u64cd\u4f5c\u3002 \u9898\u89e3 \u76ee\u524d\u60f3\u5230\u7684\u6700\u81ea\u7136\u7684\u65b9\u6cd5\uff1a \u5148\u8003\u8651\u5750\u6807(x,y)\u7684\u70b9\u7ed5\u539f\u70b9\u987a\u65f6\u9488\u8f6c90\u5ea6\u540e\u7684\u5750\u6807\uff0c\u56e0\u4e3a&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[32,51],"class_list":["post-477","post","type-post","status-publish","format-standard","hentry","category-10","tag-hackerrank","tag-leetcode"],"_links":{"self":[{"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/posts\/477","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=477"}],"version-history":[{"count":17,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/posts\/477\/revisions"}],"predecessor-version":[{"id":2712,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/posts\/477\/revisions\/2712"}],"wp:attachment":[{"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}