{"id":5875,"date":"2020-07-12T18:20:27","date_gmt":"2020-07-12T10:20:27","guid":{"rendered":"https:\/\/vip.fxxkpython.com\/?p=5875"},"modified":"2020-07-12T18:20:36","modified_gmt":"2020-07-12T10:20:36","slug":"python-%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84%e5%92%8c%e7%ae%97%e6%b3%95-03-%e5%a4%a7-o-%e8%a1%a8%e7%a4%ba%e6%b3%95%e7%9a%84-python-%e4%bb%a3%e7%a0%81%e7%a4%ba%e4%be%8b","status":"publish","type":"post","link":"https:\/\/vip.fxxkpython.com\/?p=5875","title":{"rendered":"Python \u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5 03| \u5927 O \u8868\u793a\u6cd5\u7684 Python \u4ee3\u7801\u793a\u4f8b"},"content":{"rendered":"\n<p>\u5728\u524d\u4e00\u7bc7\u4e2d\uff0c\u76f8\u4fe1\u4f60\u5bf9\u5927O\u8868\u793a\u4ee3\u7801\u590d\u6742\u5ea6\u7684\u65b9\u6cd5\u6709\u4e86\u4e00\u5b9a\u7684\u4e86\u89e3\u4e86\uff0c\u63a5\u4e0b\u6765\u5c0f\u5e05b\u4f1a\u7ee7\u7eed\u8ddf\u4f60\u8bf4\u4e00\u4e0b\u8fd9\u4e9b\u5e38\u7528\u8868\u793a\u65b9\u6cd5\u5bf9\u5e94\u7684 Python \u4ee3\u7801\u793a\u4f8b\uff0c\u8ba9\u4f60\u4e4b\u540e\u80fd\u591f\u66f4\u52a0\u6e05\u6670\u7684\u53bb\u5206\u6790\u67d0\u4e00\u6bb5\u7684\u4ee3\u7801\u590d\u6742\u5ea6\u3002<\/p>\n\n\n\n<h2>\u5927 O \u8868\u793a\u6cd5\u7684 Python \u7b80\u5355\u4ee3\u7801\u793a\u4f8b<\/h2>\n\n\n\n<h3>O(1)<\/h3>\n\n\n\n<p>\u6211\u4eec\u4e00\u822c\u5728\u5199\u7684\u4ee3\u7801\uff0c\u6ca1\u4e00\u884c\u4f1a\u6267\u884c\u4e00\u6b21\uff0c\u662f\u5e38\u6570\u7ea7\u522b\u7684\uff0c\u6bd4\u5982\u8fd9\u6837\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" width=\"152\" height=\"103\" src=\"https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2020\/07\/Screenshot-from-2020-07-12-17-10-56.png\" alt=\"\" class=\"wp-image-5876\" srcset=\"https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2020\/07\/Screenshot-from-2020-07-12-17-10-56.png 152w, https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2020\/07\/Screenshot-from-2020-07-12-17-10-56-150x103.png 150w\" sizes=\"(max-width: 152px) 100vw, 152px\" \/><\/figure><\/div>\n\n\n\n<p>\u8fd9\u6837\u662f\u4e0d\u662f\u6267\u884c\u4e86 4 \u6b21\uff0c\u6240\u4ee5\u8bf4\u8fd9\u6bb5\u4ee3\u7801\u7684\u590d\u6742\u5ea6\u662f O(4) , \u53c8\u7531\u4e8e\u4ee3\u7801\u7684\u590d\u6742\u5ea6\u8868\u793a\u7684\u662f\u968f\u7740\u4ee3\u7801\u7684\u589e\u901f\u548c\u4ee3\u7801\u6267\u884c\u6b21\u6570\u7684\u5173\u7cfb\uff0c\u6240\u4ee5\u8fd9\u65f6\u5019 O(4) \u5c31\u53ef\u4ee5\u76f4\u63a5\u8868\u793a\u4e3a O(1)\uff0c\u53ea\u8981\u6b21\u6570\u4e0d\u662f\u5230\u8fbe n \u7684\u89c4\u6a21\uff0c\u6211\u4eec\u90fd\u4f1a\u628a\u8fd9\u79cd\u8fd0\u884c\u5e38\u6570\u91cf\u7ea7\u522b\u7684\u4ee3\u7801\u8868\u793a\u4e3a O(1), \u6bd4\u5982\u8fd9\u4e2a\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" width=\"257\" height=\"44\" src=\"https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2020\/07\/Screenshot-from-2020-07-12-17-16-11.png\" alt=\"\" class=\"wp-image-5877\"\/><\/figure><\/div>\n\n\n\n<p>\u5c3d\u7ba1\u4ee3\u7801\u6267\u884c\u4e86 100000 \u6b21\uff0c O(100000) \u4e5f\u662f\u7528 O(1) \u6765\u8868\u793a\u3002<\/p>\n\n\n\n<h3 id=\"mce_2\">O(n)<\/h3>\n\n\n\n<p>\u800c\u4ee3\u7801\u7684\u6267\u884c\u6b21\u6570\u8fbe\u5230\u4e86 n \u7684\u7ea7\u522b\uff0c\u6211\u4eec\u624d\u4f7f\u7528 O(n) \u6765\u8868\u793a\u5b83\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" width=\"281\" height=\"56\" src=\"https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2020\/07\/Screenshot-from-2020-07-12-17-18-41.png\" alt=\"\" class=\"wp-image-5878\"\/><\/figure><\/div>\n\n\n\n<h3 id=\"mce_10\">O(n\u00b2)<\/h3>\n\n\n\n<p>\u770b\u8fd9\u79cd\u5d4c\u5957\u5faa\u73af\u7684\u4ee3\u7801\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" width=\"334\" height=\"181\" src=\"https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2020\/07\/Screenshot-from-2020-07-12-17-21-11.png\" alt=\"\" class=\"wp-image-5879\" srcset=\"https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2020\/07\/Screenshot-from-2020-07-12-17-21-11.png 334w, https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2020\/07\/Screenshot-from-2020-07-12-17-21-11-300x163.png 300w\" sizes=\"(max-width: 334px) 100vw, 334px\" \/><\/figure><\/div>\n\n\n\n<p>\u5728\u7b2c\u4e00\u5c42\u5faa\u73af\u7684\u65f6\u5019\uff0c\u590d\u6742\u5ea6\u662f O(n) ,  \u4f46\u6bcf\u4e00\u6b21\u5faa\u73af\u53c8\u505a\u4e86 n \u6b21\u5faa\u73af\uff0c\u6240\u4ee5\u8fd9\u6bb5\u4ee3\u7801\u7684\u590d\u6742\u5ea6\u662f O(n*n), \u4e5f\u5c31\u662f O(n\u00b2)\u3002<\/p>\n\n\n\n<h3 id=\"mce_15\">O(logN)<\/h3>\n\n\n\n<p>\u6211\u4eec\u6765\u770b\u8fd9\u6bb5\u4ee3\u7801\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" width=\"300\" height=\"228\" src=\"https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2020\/07\/Screenshot-from-2020-07-12-17-27-16.png\" alt=\"\" class=\"wp-image-5880\"\/><\/figure><\/div>\n\n\n\n<p>\u4f60\u89c9\u5f97\u8f93\u51fa\u7684\u662f\u591a\u5c11\u5462\uff1f<\/p>\n\n\n\n<p>\u8fd9\u91cc\u6211\u4eec\u7528\u5230\u4e86 while \u5faa\u73af\uff0c\u53ea\u8981\u53d1\u73b0 sum &lt; n , \u5c31\u8ba9\u5b83 * 2\uff0c\u76f4\u5230 sum >= n \u7684\u65f6\u5019\u8df3\u51fa\u5faa\u73af\uff0c\u90a3\u4e48\u9700\u8981\u6267\u884c\u591a\u5c11\u6b21\u5462\uff1f<\/p>\n\n\n\n<p>\u56e0\u4e3a\u6bcf\u6b21\u90fd\u662f * 2 \uff0c\u90a3\u4e48\u5e94\u8be5\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>2 \u7684 \uff08\u6267\u884c\u6b21\u6570\uff09 \u7684\u6b21\u65b9 = n<\/p><\/blockquote>\n\n\n\n<p>\u901a\u8fc7 log \u5c31\u53ef\u4ee5\u8fd9\u6837\u8868\u793a\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u6267\u884c\u6b21\u6570 = log(n)<\/p><\/blockquote>\n\n\n\n<p>\u6240\u4ee5\u8fd9\u6bb5\u4ee3\u7801\u7684\u590d\u6742\u5ea6\u5c31\u662f O(logN)<\/p>\n\n\n\n<h3 id=\"mce_20\">O(nlogN)<\/h3>\n\n\n\n<p>\u90a3\u4e48\u628a\u4e0a\u9762\u8fd9\u6bb5\u4ee3\u7801\u6267\u884c n \u6b21, \u590d\u6742\u5ea6\u5c31\u662f n*log(n) \u4e86\uff1a<div style=\"text-align:center;border:1px dashed #FF9A9A;padding:8px;margin:10px auto;color:#FF6666;\">\u672c\u6587\u9690\u85cf\u5185\u5bb9 <a href=\"https:\/\/vip.fxxkpython.com\/wp-login.php?redirect_to=https%3A%2F%2Fvip.fxxkpython.com%2F%3Fp%3D5875\">\u767b\u9646<\/a> \u540e\u624d\u53ef\u4ee5\u6d4f\u89c8<\/div>\u4f46\u5176\u5b9e\uff0c\u4ee3\u7801\u7684\u590d\u6742\u5ea6\u8981\u63cf\u8ff0\u7684\u662f\u589e\u901f\uff0c\u548c\u6b21\u6570\u65e0\u5173\uff0c\u6240\u4ee5\u6211\u4eec\u53ea\u9700\u8981\u5173\u6ce8\u6700\u590d\u6742\u7684\u5c31\u53ef\u4ee5\u4e86\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u4e0a\u8ff0\u90a3\u4e24\u4e2a\u7684\u590d\u6742\u5ea6\u53ea\u8981\u8fd9\u6837\u8868\u793a\u5c31\u53ef\u4ee5\u4e86\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>O(3+n) = O(n)<\/p><p>O(3+n\u00b2+n) = O(n\u00b2)<\/p><\/blockquote>\n\n\n\n<p>ok, \u4ee5\u4e0a\u7684\u539f\u7406\uff0c\u540c\u6837\u9002\u7528\u4e8e\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u5206\u6790\uff0c\u4e0b\u4e00\u7bc7\u518d\u89c1\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u524d\u4e00\u7bc7\u4e2d\uff0c\u76f8\u4fe1\u4f60\u5bf9\u5927O\u8868\u793a\u4ee3\u7801\u590d\u6742\u5ea6\u7684\u65b9\u6cd5\u6709\u4e86\u4e00\u5b9a\u7684\u4e86\u89e3\u4e86\uff0c\u63a5\u4e0b\u6765\u5c0f\u5e05b\u4f1a\u7ee7\u7eed\u8ddf\u4f60\u8bf4\u4e00\u4e0b\u8fd9\u4e9b\u5e38\u7528\u8868\u793a\u65b9\u6cd5\u5bf9\u5e94\u7684 Python \u4ee3\u7801\u793a\u4f8b\uff0c\u8ba9\u4f60\u4e4b\u540e\u80fd\u591f\u66f4\u52a0\u6e05\u6670\u7684\u53bb\u5206\u6790\u67d0\u4e00\u6bb5\u7684\u4ee3\u7801\u590d\u6742\u5ea6\u3002 \u5927 O \u8868\u793a\u6cd5\u7684 Python \u7b80\u5355\u4ee3\u7801\u793a\u4f8b O(1) \u6211\u4eec\u4e00&hellip;<\/p>\n","protected":false},"author":1,"featured_media":5882,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[475],"tags":[19,485,484,482,306],"_links":{"self":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts\/5875"}],"collection":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5875"}],"version-history":[{"count":1,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts\/5875\/revisions"}],"predecessor-version":[{"id":5887,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts\/5875\/revisions\/5887"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/media\/5882"}],"wp:attachment":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5875"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5875"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5875"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}