LeetCode: Divide Two Integers

很多bug还不清楚。。用了网上答案

 1 class Solution {
 2 public:
 3     int divide(int dividend, int divisor) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         bool isnegtive = false;
 7         int multiply = 0;
 8         long long a = dividend;
 9         long long b = divisor;
10         if (a < 0) {
11             a = -a;
12             isnegtive = !isnegtive;
13         }
14         if (b < 0) {
15             b = -b;
16             isnegtive = !isnegtive;
17         }
18         while (b << multiply <= a) {
19             multiply++;
20         }
21         int ret = 0;
22         for (int i = multiply-1; i >= 0; i--) {
23             if (b << i <= a) {
24                 ret = ret | (1 << i);
25                 a -= b << i;
26             }
27         }
28         if (isnegtive) ret = -ret;
29         return ret;
30     }
31 };
原文地址:https://www.cnblogs.com/yingzhongwen/p/2971983.html