# 29. Divide Two Integers

----------------------------------------------------------------------------

Mean:

analyse:

Time complexity: O(N)

view code

/**
* -----------------------------------------------------------------
* -----------------------------------------------------------------
*       Author: crazyacking
*       Date  : 2016-02-19-17.45
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long( LL);
typedef unsigned long long( ULL);
const double eps( 1e-8);

class Solution
{
public :
int divide( int dividend , int divisor)
{
if( ! divisor || ( dividend == INT_MIN && divisor ==- 1))
return INT_MAX;
bool sign =( dividend < 0) ^( divisor < 0);
long long dvd = labs( dividend);
long long dvs = labs( divisor);
int res = 0;
while( dvd >= dvs)
{
long long tmp = dvs , multiple = 1;
while( dvd >=( tmp << 1))
{
tmp <<= 1;
multiple <<= 1;
}
dvd -= tmp;
res += multiple;
}
return sign ?- res: res;
}
};

int main()
{
Solution solution;
int a ,b;
while( cin >> a >>b)
{
cout << solution . divide( a ,b) << endl;
}
return 0;
}
/*

*/

|
11月前
|

LeetCode刷题---Two Sum（一）
LeetCode刷题---Two Sum（一）
|
9月前
Leetcode 4. Median of Two Sorted Arrays

21 0
|
11月前
|

|

LeetCode - #2 Add Two Numbers

64 0
|

LeetCode - #1 Two Sum

48 0
LeetCode 167 Two Sum II - Input array is sorted（输入已排序数组，求其中两个数的和等于给定的数）

74 0
|
Python
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists
81 0
|

LeetCode 350. 两个数组的交集 II ntersection of Two Arrays II
LeetCode 350. 两个数组的交集 II ntersection of Two Arrays II
95 0
LeetCode contest 177 5169. 日期之间隔几天 Number of Days Between Two Dates
LeetCode contest 177 5169. 日期之间隔几天 Number of Days Between Two Dates
88 0
|

LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
105 0