{"id":2170,"date":"2016-09-07T20:04:16","date_gmt":"2016-09-07T12:04:16","guid":{"rendered":"http:\/\/boweihe.me\/?p=2170"},"modified":"2016-09-07T20:04:16","modified_gmt":"2016-09-07T12:04:16","slug":"leetcode-29-divide-two-integers","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2170","title":{"rendered":"LeetCode 29. Divide Two Integers"},"content":{"rendered":"<p>Divide two integers without using multiplication, division and mod operator.<br \/>\nIf it is overflow, return MAX_INT.<\/p>\n<h3>\u601d\u8def<\/h3>\n<p>\u8fb9\u754c\u6761\u4ef6\uff1a\u6bd4\u5982\u88ab\u9664\u6570\u3001\u9664\u6570\u662f0\uff0c\u53e6\u5916\u7ed3\u679c\u7684\u6b63\u8d1f\u53f7\u8981\u6ce8\u610f\u3002<br \/>\n\u9664\u6cd5\u53ef\u4ee5\u7528\u51cf\u6cd5\u5b9e\u73b0\uff0c\u4e0d\u8fc7\u5982\u679c\u5e72\u51cf\u7684\u8bdd\u4f1a\u5f88\u8d39\u65f6\u95f4\uff0c\u6240\u4ee5\u8981\u60f3\u60f3\u600e\u4e48\u52a0\u901f\u3002<br \/>\n\u5bb9\u6613\u77e5\u9053\uff0c\u4e00\u4e2a\u6570\u5de6\u79fb\u4e00\u4f4d\u76f8\u5f53\u4e8e\u4e58\u4ee52\u3002\u6211\u4eec\u53ef\u4ee5\u4e00\u6b21\u6b21\u628a\u9664\u6570\u4e58\u4ee52\u8bd5\u63a2\u4e00\u4e0b\uff0c\u5230\u5e95\u6700\u591a\u80fd\u51cf\u6389<em> 2^n*\u9664\u6570<\/em> \u591a\u5c11\u6b21\uff0c\u8fd9\u5c31\u76f8\u5f53\u4e8e\u505a\u4e86<em>2^n<\/em>\u6b21\u9664\u6cd5\u3002\u7531\u4e8e\u8bd5\u63a2\u7684\u65f6\u5019\u4e00\u6b21\u6b21\u505a\u4e86\u4e58\u65b9\uff0c\u6240\u4ee5\u8bd5\u63a2\u7684\u6b21\u6570\u662flog\u7ea7\u522b\u7684\uff0c\u6bd4\u539f\u6765\u4e00\u4e2a\u4e2a\u51cf\u8981\u5feb\u591a\u4e86\u3002<\/p>\n<h3>\u4ee3\u7801<\/h3>\n<pre class=\"lang:c++ decode:true \">class Solution {\npublic:\n\tint divide(int dividend, int divisor) {\n\t\tif (dividend == 0)\n\t\t\treturn 0;\n\t\tif (divisor == 0)\n\t\t\treturn INT_MAX;\n\t\tbool posResult = true;\n\t\tif ((dividend &gt; 0 &amp;&amp; divisor &lt; 0) || (dividend &lt; 0 &amp;&amp; divisor &gt;0))\n\t\t\tposResult = false;\n\t\tlong long lDividend = dividend;\n\t\tif (lDividend &lt; 0)\n\t\t\tlDividend = ~(lDividend-1);\n\t\tlong long lDivisor = divisor;\n\t\tif (lDivisor &lt; 0)\n\t\t\tlDivisor = ~(lDivisor-1);\n\t\tlong long currDivisor = lDivisor;\n\t\tlong long accRate = 1;\n\t\tlong long result = 0;\n\t\twhile (lDividend &gt;= lDivisor) {\n\t\t\twhile ((currDivisor &lt;&lt; 1) &lt; lDividend) {\n\t\t\t\tcurrDivisor &lt;&lt;= 1;\n\t\t\t\taccRate &lt;&lt;= 1;\n\t\t\t}\n\t\t\twhile (currDivisor &gt; lDividend) {\n\t\t\t\tcurrDivisor &gt;&gt;= 1;\n\t\t\t\taccRate &gt;&gt;= 1;\n\t\t\t}\n\t\t\tlDividend -= currDivisor;\n\t\t\tresult += accRate;\n\t\t}\n\t\tif (posResult) {\n\t\t\tif (result &gt; INT_MAX) {\n\t\t\t\treturn INT_MAX;\n\t\t\t}\n\t\t\telse {\n\t\t\t\treturn (int)result;\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tresult = -result;\n\t\t\tif (result &lt; INT_MIN)\n\t\t\t\treturn INT_MIN;\n\t\t\telse\n\t\t\t\treturn (int)result;\n\t\t}\n\t}\n};<\/pre>\n<p>&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. \u601d\u8def \u8fb9\u754c\u6761\u4ef6\uff1a\u6bd4\u5982\u88ab\u9664\u6570\u3001\u9664\u6570\u662f0\uff0c\u53e6\u5916\u7ed3\u679c\u7684\u6b63\u8d1f\u53f7\u8981\u6ce8\u610f\u3002 \u9664\u6cd5\u53ef\u4ee5\u7528\u51cf\u6cd5\u5b9e\u73b0\uff0c\u4e0d\u8fc7\u5982\u679c\u5e72\u51cf\u7684\u8bdd\u4f1a\u5f88\u8d39\u65f6\u95f4\uff0c\u6240\u4ee5\u8981\u60f3\u60f3\u600e\u4e48\u52a0\u901f\u3002 \u5bb9\u6613\u77e5\u9053\uff0c\u4e00\u4e2a\u6570\u5de6\u79fb\u4e00\u4f4d\u76f8\u5f53\u4e8e\u4e58\u4ee52\u3002\u6211\u4eec\u53ef\u4ee5\u4e00\u6b21\u6b21\u628a\u9664\u6570\u4e58\u4ee52\u8bd5\u63a2\u4e00\u4e0b\uff0c\u5230\u5e95\u6700\u591a\u80fd\u51cf\u6389 2^n*\u9664\u6570 \u591a\u5c11\u6b21\uff0c\u8fd9\u5c31\u76f8\u5f53\u4e8e\u505a\u4e862^n\u6b21\u9664\u6cd5\u3002\u7531\u4e8e\u8bd5\u63a2\u7684\u65f6\u5019\u4e00\u6b21\u6b21\u505a\u4e86\u4e58\u65b9\uff0c\u6240\u4ee5\u8bd5\u63a2\u7684\u6b21\u6570\u662flog\u7ea7\u522b\u7684\uff0c\u6bd4\u539f\u6765\u4e00\u4e2a\u4e2a\u51cf\u8981\u5feb\u591a\u4e86\u3002 \u4ee3\u7801 class Solution { public: int divide(int dividend, int divisor) { if (dividend == 0) return 0; if (divisor == 0) return INT_MAX; bool posResult = true; if ((dividend &gt; 0 &amp;&amp; divisor [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2170","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2170"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2170\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}