{"id":2931,"date":"2020-02-12T16:50:50","date_gmt":"2020-02-12T08:50:50","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=2931"},"modified":"2020-02-12T16:50:50","modified_gmt":"2020-02-12T08:50:50","slug":"%e3%80%90leetcode%e3%80%910050-pow%ef%bc%88x%ef%bc%8cn%ef%bc%89","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/algorithm\/2931\/","title":{"rendered":"\u3010LeetCode\u30110050 Pow\uff08x\uff0cn\uff09"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/powx-n\/\">https:\/\/leetcode-cn.com\/problems\/powx-n\/<\/a><\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-\n<em>\n<\/em>class Solution(object):\n    def myPow(self, x, n):\n        <em>\"\"\"\n        <\/em><strong><em>:type<\/em><\/strong><em> x: float\n        <\/em><strong><em>:type<\/em><\/strong><em> n: int\n        <\/em><strong><em>:rtype<\/em><\/strong><em>: float\n        \"\"\"\n        <\/em>if n == 0:\n            return 1.0\n        n_negative = False\n        if n &lt; 0:\n            n = -n\n            n_negative = True\n        flag = 1\n        times = 0\n        while 2 * flag &lt;= n:\n            flag = flag &lt;&lt; 1\n            times += 1\n        # \u5c1d\u8bd5\u5224\u65ad\u4e0b\u4e00\u4e2a\u548c\u5f53\u524d\u54ea\u4e00\u4e2a\u79bbn\u66f4\u8fd1\n        n_is_nearly = True\n        if flag * 2 - n &lt; n - flag:\n            n_is_nearly = False\n        sum = x\n        for i in range(times):\n            sum *= sum\n        if n_is_nearly:\n            for i in range(n - flag):\n                sum *= x\n        else:\n            sum *= sum\n            for i in range(2 * flag - n):\n                sum \/= x\n        return sum if n_negative is False else 1.0 \/ sum\nif __name__ == '__main__':\n    x = 0.00001\n    n = 2147483647\n    print(Solution().myPow(x, n))\n<\/pre>\n\n\n<p><strong>\u601d\u8def\uff1a<\/strong>\u8fd9\u9053\u9898\u7b2c\u4e00\u65f6\u95f4\u60f3\u7684\u5c31\u662f\u627e\u4e00\u4e2a\u6700\u5927\u7684\u5c0f\u4e8en\u76842\u7684n\u6b21\u65b9\u6570\uff0c\u5c31\u662f\u6bd4\u59822\u768410\u6b21\u65b9\uff0c\u6211\u4eec\u5148\u7b9710\u4ee5\u5185\u6700\u5c0f\u76842\u7684n\u6b21\u65b9\u6570\uff0c\u662f8\uff0c\u8fd9\u65f6\u6211\u4eec\u9700\u8981\u8ba1\u7b973\u6b21sum*sum\u5373\u53ef\u5f97\u52302\u76848\u6b21\u65b9\uff0c\u7136\u540e\u518d\u4e582\u6b21\u5373\u53ef\u3002<\/p>\n\n\n<p>\u4f46\u662f\u6709\u4e00\u4e2a2147483647\u6b21\u65b9\u7684\u6570\u3002\u3002\u90a3\u6211\u4eec\u53ea\u80fd\u5224\u65ad2\u7684n\u6b21\u65b9\u548c2\u7684n+1\u6b21\u65b9\u548c\u5e73\u65b9\u6570\u54ea\u4e2a\u79bb\u5f97\u8fdc\u3002\u7136\u540e\u6839\u636e\u60c5\u51b5\u9009\u62e9\u4e58\u9664\u3002<\/p>\n\n\n<p><strong>\u601d\u8003\uff1a<\/strong><\/p>\n\n\n<pre class=\"wp-block-preformatted\">def myPow1(self, x, n):<br \/>    <em>\"\"\"<br \/><\/em><em>    <\/em><strong><em>:type<\/em><\/strong><em> x: float<br \/><\/em><em>    <\/em><strong><em>:type<\/em><\/strong><em> n: int<br \/><\/em><em>    <\/em><strong><em>:rtype<\/em><\/strong><em>: float<br \/><\/em><em>    \"\"\"<br \/><\/em><em>    <\/em>i = n<br \/>    if i &lt; 0: i = -i<br \/>    res = 1<br \/>    while i != 0:<br \/>        if i % 2 != 0: res *= x<br \/>        x *= x<br \/>        i = i \/\/ 2<br \/>    return res if n &gt; 0 else 1 \/ res<\/pre>\n\n\n<p>\u6709\u4e00\u4e2a\u601d\u8def\u7c7b\u4f3c\uff0c\u4f46\u662f\u7a7a\u95f4\u590d\u6742\u5ea6\u76f8\u5bf9\u8f83\u4f4e\u7684\u65b9\u6cd5\uff0c\u4e5f\u662f\u7c7b\u4f3c\u7684\u5bf9\u534a\u67e5\u627e\uff0c\u5982\u679c\u662f\u5947\u6570\u5c31\u4e58\u4e00\u4e0b\uff0c\u5076\u6570\u5c31\u6298\u534a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode-cn.com\/problems\/powx-n\/ # -*- cod [&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":1368,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2931"}],"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=2931"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2931\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2931"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}