{"id":2071,"date":"2022-01-15T11:22:57","date_gmt":"2022-01-15T03:22:57","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2071"},"modified":"2022-01-15T11:22:58","modified_gmt":"2022-01-15T03:22:58","slug":"leetcodeday29","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/15\/leetcodeday29\/","title":{"rendered":"leetcodeday29 &#8211;\u4e24\u6570\u76f8\u9664"},"content":{"rendered":"\n<p>\u7ed9\u5b9a\u4e24\u4e2a\u6574\u6570\uff0c\u88ab\u9664\u6570&nbsp;<code>dividend<\/code>&nbsp;\u548c\u9664\u6570&nbsp;<code>divisor<\/code>\u3002\u5c06\u4e24\u6570\u76f8\u9664\uff0c\u8981\u6c42\u4e0d\u4f7f\u7528\u4e58\u6cd5\u3001\u9664\u6cd5\u548c mod \u8fd0\u7b97\u7b26\u3002<\/p>\n\n\n\n<p>\u8fd4\u56de\u88ab\u9664\u6570&nbsp;<code>dividend<\/code>&nbsp;\u9664\u4ee5\u9664\u6570&nbsp;<code>divisor<\/code>&nbsp;\u5f97\u5230\u7684\u5546\u3002<\/p>\n\n\n\n<p>\u6574\u6570\u9664\u6cd5\u7684\u7ed3\u679c\u5e94\u5f53\u622a\u53bb\uff08<code>truncate<\/code>\uff09\u5176\u5c0f\u6570\u90e8\u5206\uff0c\u4f8b\u5982\uff1a<code>truncate(8.345) = 8<\/code>&nbsp;\u4ee5\u53ca&nbsp;<code>truncate(-2.7335) = -2<\/code><\/p>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165:<\/strong> dividend = 10, divisor = 3\n<strong>\u8f93\u51fa:<\/strong> 3\n<strong>\u89e3\u91ca: <\/strong>10\/3 = truncate(3.33333..) = truncate(3) = 3<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165:<\/strong> dividend = 7, divisor = -3\n<strong>\u8f93\u51fa:<\/strong> -2\n<strong>\u89e3\u91ca:<\/strong> 7\/-3 = truncate(-2.33333..) = -2<\/code><\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul><li>\u88ab\u9664\u6570\u548c\u9664\u6570\u5747\u4e3a 32 \u4f4d\u6709\u7b26\u53f7\u6574\u6570\u3002<\/li><li>\u9664\u6570\u4e0d\u4e3a\u00a00\u3002<\/li><li>\u5047\u8bbe\u6211\u4eec\u7684\u73af\u5883\u53ea\u80fd\u5b58\u50a8 32 \u4f4d\u6709\u7b26\u53f7\u6574\u6570\uff0c\u5176\u6570\u503c\u8303\u56f4\u662f [\u22122<sup>31<\/sup>,\u00a0 2<sup>31\u00a0<\/sup>\u2212 1]\u3002\u672c\u9898\u4e2d\uff0c\u5982\u679c\u9664\u6cd5\u7ed3\u679c\u6ea2\u51fa\uff0c\u5219\u8fd4\u56de 2<sup>31\u00a0<\/sup>\u2212 1\u3002<\/li><\/ul>\n\n\n\n<p>\u601d\u8def\uff1a<\/p>\n\n\n\n<p>\u4e00\u5f00\u59cb\u6253\u7b97\u7528 \u51cf\u6cd5\u5b9e\u73b0\uff0c\u4f46\u4f1a\u8d85\u65f6\uff0c\u56e0\u4e3a\u6bcf\u6b21\u90fd\u662fdividend-divisor\uff0c\u6b65\u5b50\u5f88\u5c0f\uff0c\u5bfc\u81f4\u5bf9\u4e8e\u4e00\u4e2a\u5927\u6570-\u5c0f\u6570\uff0c\u5f88\u8017\u65f6\uff0c\u56e0\u6b64\u91c7\u7528 \u6b65\u957f\u6bcf\u6b21*2\uff0c\u6bcf\u6b21\u90fd\u662f dividend-divisor \uff0c divisor =2* divisor\uff0c\u5982\u679c  dividend-2divisor &lt;0,\u90a3\u4e48 \u6b65\u957f\u5728\u53d8\u4e3a\u6700\u521d\u7684\u5927\u5c0f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># &#91;29] \u4e24\u6570\u76f8\u9664\r\n#\r\n\r\n# @lc code=start\r\nclass Solution:\r\n    def divide(self, dividend: int, divisor: int) -> int:\r\n        n,m=dividend,divisor\r\n        MAX=2**31-1\r\n        MIN=0-2**31\r\n        dividend=abs(dividend)\r\n        divisor=abs(divisor)\r\n\r\n        y=divisor\r\n        i=1\r\n        re=0\r\n        while i>0:\r\n            x=dividend-divisor\r\n            if i==1 and x&lt;0:\r\n                print(re)\r\n                break\r\n\r\n            if x>0:\r\n                dividend=x\r\n                divisor+=divisor\r\n                re=re+i\r\n                i+=i\r\n        \r\n\r\n\r\n            elif x==0:\r\n                re=re+i\r\n                print(re)\r\n                break\r\n            else:\r\n                i=1\r\n                divisor=y\r\n\r\n        if (m>0 and n>0) or (m&lt;0 and n&lt;0):\r\n            return min(re,MAX)\r\n        else:return max(0-re,MIN)\r\n\r\n\r\n\r\n# @lc code=end<\/code><\/pre>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-227.png\" alt=\"\" class=\"wp-image-2072\" width=\"349\" height=\"156\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-227.png 508w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-227-300x134.png 300w\" sizes=\"(max-width: 349px) 100vw, 349px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/w.wallhaven.cc\/full\/3z\/wallhaven-3z7dd3.png\" alt=\"\" width=\"670\" height=\"377\"\/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e24\u4e2a\u6574\u6570\uff0c\u88ab\u9664\u6570&nbsp;dividend&nbsp;\u548c\u9664\u6570&nbsp;divisor\u3002\u5c06\u4e24\u6570\u76f8\u9664\uff0c\u8981\u6c42 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/15\/leetcodeday29\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday29 &#8211;\u4e24\u6570\u76f8\u9664<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2071"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=2071"}],"version-history":[{"count":2,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2071\/revisions"}],"predecessor-version":[{"id":2074,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2071\/revisions\/2074"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2071"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}