{"id":8785,"date":"2021-11-26T13:48:08","date_gmt":"2021-11-26T21:48:08","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8785"},"modified":"2021-11-26T13:52:26","modified_gmt":"2021-11-26T21:52:26","slug":"leetcode-29-divide-two-integers","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/bit\/leetcode-29-divide-two-integers\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 29. Divide Two Integers"},"content":{"rendered":"\n<p>Given two integers&nbsp;<code>dividend<\/code>&nbsp;and&nbsp;<code>divisor<\/code>, divide two integers&nbsp;<strong>without<\/strong>&nbsp;using multiplication, division, and mod operator.<\/p>\n\n\n\n<p>The integer division should truncate toward zero, which means losing its fractional part. For example,&nbsp;<code>8.345<\/code>&nbsp;would be truncated to&nbsp;<code>8<\/code>, and&nbsp;<code>-2.7335<\/code>&nbsp;would be truncated to&nbsp;<code>-2<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>quotient<\/strong>&nbsp;after dividing&nbsp;<\/em><code>dividend<\/code><em>&nbsp;by&nbsp;<\/em><code>divisor<\/code>.<\/p>\n\n\n\n<p><strong>Note:&nbsp;<\/strong>Assume we are dealing with an environment that could only store integers within the&nbsp;<strong>32-bit<\/strong>&nbsp;signed integer range:&nbsp;<code>[\u22122<sup>31<\/sup>, 2<sup>31<\/sup>&nbsp;\u2212 1]<\/code>. For this problem, if the quotient is&nbsp;<strong>strictly greater than<\/strong>&nbsp;<code>2<sup>31<\/sup>&nbsp;- 1<\/code>, then return&nbsp;<code>2<sup>31<\/sup>&nbsp;- 1<\/code>, and if the quotient is&nbsp;<strong>strictly less than<\/strong>&nbsp;<code>-2<sup>31<\/sup><\/code>, then return&nbsp;<code>-2<sup>31<\/sup><\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> dividend = 10, divisor = 3\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> 10\/3 = 3.33333.. which is truncated to 3.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> dividend = 7, divisor = -3\n<strong>Output:<\/strong> -2\n<strong>Explanation:<\/strong> 7\/-3 = -2.33333.. which is truncated to -2.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> dividend = 0, divisor = 1\n<strong>Output:<\/strong> 0\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> dividend = 1, divisor = 1\n<strong>Output:<\/strong> 1\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>-2<sup>31<\/sup>&nbsp;&lt;= dividend, divisor &lt;= 2<sup>31<\/sup>&nbsp;- 1<\/code><\/li><li><code>divisor != 0<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Binary \/ Bit Operation<\/strong><\/h2>\n\n\n\n<p>The answer can be represented in binary format.<\/p>\n\n\n\n<p>a \/ b = c<br>a >= b * \u00a0\u03a3(d<sub>i<\/sub>*2<sup>i<\/sup>)<br>c = \u03a3(d<sub>i<\/sub>*2<sup>i<\/sup>)<\/p>\n\n\n\n<p>e.g. 1: 10 \/ 3<br>=> 10 >= 3*(2<sup>1<\/sup> + 2<sup>0<\/sup>) = 3 * (2 + 1) = 9<br>ans = 2<sup>1<\/sup> + 2<sup>0<\/sup> = 3<br><br>e.g. 2: 100 \/ 7<br>=> 100 >= 7*(2<sup>3<\/sup> + 2<sup>2<\/sup>+2<sup>1<\/sup>) = 7 * (8 + 4 + 2) = 7 * 14 = 98<br>ans = 2<sup>3<\/sup>+2<sup>2<\/sup>+2<sup>1<\/sup>=8+4+2=14<\/p>\n\n\n\n<p>Time complexity: O(32)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int divide(int A, int B) {\n    if (B == INT_MIN)\n      return A == INT_MIN ? 1 : 0;\n    if (A == INT_MIN) {\n      if (B == -1) return INT_MAX;\n      return B > 0 ? divide(A + B, B) - 1 : divide(A - B, B) + 1;\n    }    \n    int a = abs(A);\n    int b = abs(B);\n    int ans = 0;\n    for (int x = 31; x >= 0; --x)\n      if (a >> x >= b)\n        ans += 1 << x, a -= b << x;\n    return (A > 0) == (B > 0) ? ans : -ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given two integers&nbsp;dividend&nbsp;and&nbsp;divisor, divide two integers&nbsp;without&nbsp;using multiplication, division, and mod operator. The integer division should truncate toward zero, which means losing its fractional part. For&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[39,16,177],"class_list":["post-8785","post","type-post","status-publish","format-standard","hentry","category-bit","tag-binary","tag-bit","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8785","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8785"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8785\/revisions"}],"predecessor-version":[{"id":8788,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8785\/revisions\/8788"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8785"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8785"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8785"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}