{"id":2826,"date":"2020-01-28T14:09:49","date_gmt":"2020-01-28T06:09:49","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=2826"},"modified":"2020-01-28T14:09:49","modified_gmt":"2020-01-28T06:09:49","slug":"%e3%80%90leetcode%e3%80%910029-%e4%b8%a4%e6%95%b0%e7%9b%b8%e9%99%a4","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/algorithm\/2826\/","title":{"rendered":"\u3010LeetCode\u30110029 \u4e24\u6570\u76f8\u9664"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/divide-two-integers\/\">https:\/\/leetcode-cn.com\/problems\/divide-two-integers\/<\/a><\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-\n<em>\n<\/em>class Solution(object):\n    def divide(self, dividend, divisor):\n        <em>\"\"\"\n        <\/em><strong><em>:type<\/em><\/strong><em> dividend: int\n        <\/em><strong><em>:type<\/em><\/strong><em> divisor: int\n        <\/em><strong><em>:rtype<\/em><\/strong><em>: int\n        \"\"\"\n        <\/em>negative = False\n        res = 0\n        count = 0\n        if divisor &lt; 0 &lt; dividend:\n            negative = True\n        if dividend &lt; 0 &lt; divisor:\n            negative = True\n        dividend = abs(dividend)\n        divisor = abs(divisor)\n        while dividend >= divisor:\n            count += 1\n            divisor &lt;&lt;= 1\n        while count > 0:\n            count -= 1\n            divisor >>= 1\n            res &lt;&lt;= 1\n            if dividend >= divisor:\n                res += 1\n                dividend -= divisor\n        if negative:\n            res = -res\n        return res if -(1 &lt;&lt; 31) &lt;= res &lt;= (1 &lt;&lt; 31)-1 else (1 &lt;&lt; 31) - 1\nif __name__ == '__main__':\n    dividend = -2147483648\n    divisor = 1\n    # dividend = 10\n    # divisor = 3\n    print(Solution().divide(dividend, divisor))\n<\/pre>\n\n\n<p><strong>\u601d\u8def<\/strong>\uff1a\u4e8c\u8fdb\u5236\u504f\u79fb\uff0c\u56e0\u4e3a\u4e0d\u8ba9\u7528\u4e58\u9664\u6cd5\uff0c\u6240\u4ee5\u91c7\u7528\u7c7b\u4f3c\u7684\u4e8c\u8fdb\u5236\u504f\u79fb\u8ba1\u7b97\uff0c\u5de6\u79fb\u76f8\u5f53\u4e8e\u4e58\u4e8c\uff0c\u53f3\u79fb\u76f8\u5f53\u4e8e\u9664\u4e8c\u3002<\/p>\n\n\n<p>\u6211\u4eec\u4e3e\u4e00\u4e2a20\/4\u7684\u4f8b\u5b50\u3002\u8bbed=20\uff0cr=4\uff0cresult\uff08res\uff09\u521d\u59cb\u5316\u4e3a0\u3002<\/p>\n\n\n<p>\u9996\u5148\u6211\u4eec\u5c06r\u5de6\u79fb\u76f4\u81f3\u5927\u4e8ed\uff0c\u4e8e\u662f\u6211\u4eec\u67098\uff0c16\uff0c32\u3002\u7ecf\u8fc7\u4e09\u6b21\u5de6\u79fbr=32>d=20\u3002<\/p>\n\n\n<p>\u7136\u540e\u6211\u4eec\u8fdb\u884c\u9664\u6cd5\u8ba1\u7b97\u3002<\/p>\n\n\n<p>\u9996\u5148\uff1a<\/p>\n\n\n<p>r\u53f3\u79fb\uff0c\u7136\u540e\u5224\u65add\u662f\u5426\u5927\u4e8er\uff0c\u5982\u679c\u5927\u4e8er\u8bf4\u660eres\u4e2d\u5f53\u524d\u4f4d\u4e3a1\u3002<\/p>\n\n\n<p>\u663e\u713620>16\uff0c\u56e0\u6b64res+=1\uff08res=1\uff09\uff0c\u7136\u540ed = d-r = 20-16=4\u3002<\/p>\n\n\n<p>\u7ee7\u7eed\u4e0b\u4e00\u6b65\uff1a<\/p>\n\n\n<p>r\u53f3\u79fb=8\uff0c\u663e\u7136d=4&lt;r=8\uff0cres\u53f3\u79fb\u4f46\u662f\u4e0d\u52a0\u4e00\uff0cres=10\uff08\u4e8c\u8fdb\u5236\uff09,d<\/p>\n\n\n<p>\u4e0d\u53d8\u3002<\/p>\n\n\n<p>\u4e0b\u4e00\u6b65\uff1a<\/p>\n\n\n<p>r\u53f3\u79fb=4\uff0c\u663e\u7136d=4=r=4\uff0cres\u53f3\u79fb\u52a0\u4e00\uff0cres=101\uff08\u4e8c\u8fdb\u5236\uff09,d = d &#8211; 4 = 0\uff08\u5982\u679c\u4e0d\u80fd\u6574\u9664\u8fd9\u91cc\u4e5f\u5c31\u4e0d\u662f0\uff0c\u4f46\u662f\u4e00\u5b9a\u5c0f\u4e8e4\uff0c\u56e0\u4e3a\u5982\u679c\u8fd9\u91cc\u5927\u4e8e4\u90a3\u4e48\u5728\u8ba1\u7b97\u4e4b\u524dr\u5c31\u5927\u4e8e8\u4e86\uff0c\u5728\u4e0a\u4e00\u6b65\u5c31\u5904\u7406\u6389\u4e86\uff09\u3002<\/p>\n\n\n<p>\u7531\u4e8e\u6211\u4eec\u53ea\u5bf9r\u8fdb\u884c\u4e86\u4e09\u6b21\u53f3\u79fb\uff0c\u56e0\u6b64\u5230\u8fd9\u91cc\u8ba1\u7b97\u7ed3\u675f\uff0c\u7ed3\u679cres=101=5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode-cn.com\/problems\/divide-two-intege [&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":1403,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2826"}],"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=2826"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2826\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2826"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2826"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2826"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}