{"id":24515737,"url":"https://github.com/yanglr/beattheoffer","last_synced_at":"2026-02-21T14:05:06.810Z","repository":{"id":70228693,"uuid":"64527839","full_name":"yanglr/BeatTheOffer","owner":"yanglr","description":"剑指Offer2014 - 题解(C++版)","archived":false,"fork":false,"pushed_at":"2016-07-30T06:03:13.000Z","size":6,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-24T18:58:23.317Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://yanglr.gitbooks.io/beattheoffer-solutions/content/","language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yanglr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-07-30T05:22:48.000Z","updated_at":"2016-07-30T06:03:14.000Z","dependencies_parsed_at":"2023-02-27T11:00:33.026Z","dependency_job_id":null,"html_url":"https://github.com/yanglr/BeatTheOffer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/yanglr/BeatTheOffer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yanglr%2FBeatTheOffer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yanglr%2FBeatTheOffer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yanglr%2FBeatTheOffer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yanglr%2FBeatTheOffer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yanglr","download_url":"https://codeload.github.com/yanglr/BeatTheOffer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yanglr%2FBeatTheOffer/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29682749,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-21T13:29:26.630Z","status":"ssl_error","status_checked_at":"2026-02-21T13:26:50.125Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-22T01:19:01.490Z","updated_at":"2026-02-21T14:05:06.787Z","avatar_url":"https://github.com/yanglr.png","language":"HTML","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 剑指Offer2014 - 题解 {#offer2014}\n\n**面试题：数的整数次方**\n\n**温馨提示：**本技术博客的相关代码将会在github([https://github.com/yanglr](https://github.com/yanglr))中同步更新，敬请star和fork...\n\n**题目：**实现函数double Power(double base, int exponent), 求base的exponent次方。不得使用库函数，同时**不需要考虑大数问题**。\n\n其中base为浮点数，而exponent为整数(可正可负，可为0).\n\n提交网址： [http://www.nowcoder.com/practice/1a834e5e3e1a4b7ba251417554e07c00?tpId=13\u0026amp;tqId=11165](http://www.nowcoder.com/practice/1a834e5e3e1a4b7ba251417554e07c00?tpId=13\u0026tqId=11165)\n\n**分析：**\n\n二分求幂(时间复杂度为log n)，使用二分法则问题可转化为：\n\n![](http://img.blog.csdn.net/20160507003255004)\n\n此公式并不能很好的反映情况，且用n=2k+1表示**奇数**时，**(**n\u003csub\u003e奇\u003c/sub\u003e**-1)/2=k=**⌊n\u003csub\u003e奇\u003c/sub\u003e/2⌋ (k∈Z)，故以上公式可改进成如下公式：\n\n![](http://img.blog.csdn.net/20160514130818081)\n\n其中 ![](http://img.blog.csdn.net/20160514132111631)，不论n是奇是偶，对其折半后下取整即可得到k，而在编程语言中如果变量类型为整型，k=n/2，如果用弱数据类型的语言(比如：PHP、[Python](http://lib.csdn.net/base/11)、JavaScript等)实现此方法，需自行ceiling或ceil进行下取整！\n\n当选取某种强数据类型的编程语言，且上述公式中的变量n类型为整型时，可继续简化： ![](http://img.blog.csdn.net/20160507074543378?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)\n\n这样，比较容易理解，也有利于写出清晰结构的代码...\n\n非递归实现 AC代码：\n\n\u003cpre class=\"cpp\"\u003e#include\u0026lt;cstdio\u0026gt;\nusing namespace std;\nclass Solution {\npublic:\n    double Power(double base, int exponent) {\n        if(exponent \u0026lt; 0)  // 将指数exponent\u0026lt;0的情形规约为exponent\u0026gt;0的情形\n        {\n            exponent = -exponent;\n            base = 1.0/base;\n        }\n        double res = 1.0;\n        for(; exponent \u0026gt; 0; exponent \u0026gt;\u0026gt;= 1)  // 指数每次折半，进行迭代\n        {\n            if((exponent \u0026amp; 0x1) == 1)  res *= base;  // a \u0026amp; 0x1 等价于 a%2，用来判断a的奇偶性\n            base *= base;  // 底数base替换为base^2\n        }\n        return res;\n    }\n};\n\n// 以下为测试\nint main()\n{\n\tSolution sol;\n\tdouble num1=sol.Power(20, 2);\n\tdouble num2=sol.Power(13, -2);\n\tdouble num3=sol.Power(-10, 3);\n\tdouble num4=sol.Power(0, 0);\t\n\tprintf(\u0026quot;%f\\n\u0026quot;,num1); \n\tprintf(\u0026quot;%f\\n\u0026quot;,num2);\n\tprintf(\u0026quot;%f\\n\u0026quot;,num3);\t\t\n\tprintf(\u0026quot;%d\\n\u0026quot;,num4);\t\t\n\treturn 0;\n}\u003c/pre\u003e\n\n剑指offer上不需要考虑大数问题，可是Leetcode上需要考虑大数问题、边界值问题...\n\n50\\. Pow(x, n)\n\nTotal Accepted: 90298 Total Submissions: 323977 Difficulty: Medium    ACrate: 27.9%\n\nImplement pow(x, n).\n\n提交网址： [https://leetcode.com/problems/powx-n/](https://leetcode.com/problems/powx-n/)\n\n刚开始Leetcode上越界条件怎么都过不去，每次都是 **299 / 300** test cases passed....\n\n**输入输出样例：**\n\nInput: 2.00000 -2147483648 Output: 0.00000 ---------------- Input: 1.00000 -2147483648 Output: 1.00000 ---------------- Input: -1.00000 2147483647 Output: -1.00000 ---------------- Input: -1.00000 -2147483648 Output: 1.00000 ---------------- Input: 2.00000 -2147483648 Output: 0.00000 LeetCode AC代码:\n\n\u003cpre class=\"cpp\"\u003e#include\u0026lt;cstdio\u0026gt;\n#include\u0026lt;climits\u0026gt;     // INT_MAX和INT_MAX在C语言limits.h中定义，而float.h 定义了double、float类型数的最大最小值 \nusing namespace std;\nclass Solution {\npublic:\n    double myPow(double x, int n)\n    {\n        if(x==0 \u0026amp;\u0026amp; n==0) return 1;    \n        if((n\u0026lt;=INT_MIN || n\u0026gt;=INT_MAX) \u0026amp;\u0026amp; (x\u0026gt;1 || x\u0026lt;-1)) return 0;\n        if(x==1 \u0026amp;\u0026amp; n==INT_MIN) return 1;  // 加的超强补丁   \n        if(n\u0026gt;=INT_MAX \u0026amp;\u0026amp; (x==1 || x==-1)) return x;\n        if(n\u0026lt;=INT_MIN \u0026amp;\u0026amp; (x==1 || x==-1)) return -x;\n\n        if(n \u0026lt; 0)\n        {\n            n = -n;\n            x = 1.0/x;\n        }\n        double res = 1.0;\n        for(; n \u0026gt; 0; n \u0026gt;\u0026gt;= 1)\n        {\n            if((n \u0026amp; 0x1) == 1)  res *= x;\n            x *= x;  // 底数x替换为x^2\n        }\n        return res;\n    }\n};\n\n// 以下为测试\nint main()\n{\n\tSolution sol;\n\tdouble res1=sol.myPow(0, 0);\t\n\tdouble res2=sol.myPow(13, -2);\n\tdouble res3=sol.myPow(-10, 0);\n\tdouble res4=sol.myPow(20, 2);\t\n\tdouble res5=sol.myPow(2.00000,-2147483648);\n\tdouble res6=sol.myPow(1.00000,-2147483648);\n\tdouble res7=sol.myPow(-1.00000,2147483647);\n\tdouble res8=sol.myPow(-1.00000,-2147483648);\n\tdouble res9=sol.myPow(2.00000,-2147483648);\n\n\tprintf(\u0026quot;%f\\n\u0026quot;, res1); \n\tprintf(\u0026quot;%f\\n\u0026quot;, res2);\n\tprintf(\u0026quot;%f\\n\u0026quot;, res3);\t\t\n\tprintf(\u0026quot;%f\\n\u0026quot;, res4);\n\tprintf(\u0026quot;%f\\n\u0026quot;, res5);\n\tprintf(\u0026quot;%f\\n\u0026quot;, res6);\t\t\n\tprintf(\u0026quot;%f\\n\u0026quot;, res7);\n\tprintf(\u0026quot;%f\\n\u0026quot;, res8);\n\tprintf(\u0026quot;%f\\n\u0026quot;, res9);\t\t\t\t\n\treturn 0;\n}\u003c/pre\u003e\n\n相关链接：\n\n剑指offer-面试题11：数值的整数次方 [https://www.douban.com/note/355223813/](https://www.douban.com/note/355223813/)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyanglr%2Fbeattheoffer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyanglr%2Fbeattheoffer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyanglr%2Fbeattheoffer/lists"}