# [LeetCode]29.Divide Two Integers

#### 【题目】

Divide two integers without using multiplication, division and mod operator.

If it is overflow, return MAX_INT.

#### 【代码】

/*********************************
*   日期：2015-01-24
*   作者：SJF0115
*   题目: 29.Divide Two Integers
*   网址：https://oj.leetcode.com/problems/divide-two-integers/
*   结果：AC
*   来源：Time Limit Exceeded
*   博客：
**********************************/
#include <iostream>
using namespace std;

class Solution {
public:
int divide(int dividend, int divisor) {
// 当dividend=INt_MAX时，-dividend会溢出,用long long
long long a = dividend >= 0 ? dividend : -(long long)dividend;
// divisor=INt_MAX时，-divisor,用long long
long long b = divisor >= 0 ? divisor : -(long long)divisor;
int count = 0;
// 不断减
while(a >= b){
a -= b;
count ++;
}//while
// 正负
int isPositive = (dividend ^ divisor) >> 31;
if(isPositive == 0){
return count;
}//if
else{
return -count;
}//else
}
};

int main(){
Solution solution;
int dividend = -2147483648;
int divisor = -1;
int result = solution.divide(dividend,divisor);
// 输出
cout<<result<<endl;
return 0;
}



#### 【代码二】

/*********************************
*   日期：2015-01-25
*   作者：SJF0115
*   题目: 29.Divide Two Integers
*   网址：https://oj.leetcode.com/problems/divide-two-integers/
*   结果：AC
*   来源：Time Limit Exceeded
*   博客：
**********************************/
#include <iostream>
#include <climits>
using namespace std;

class Solution {
public:
int divide(int dividend, int divisor) {
// 当dividend=INt_MAX时，-dividend会溢出,用long long
long long a = dividend >= 0 ? dividend : -(long long)dividend;
// 当divisor=INt_MAX时，-divisor会溢出,用long long
long long b = divisor >= 0 ? divisor : -(long long)divisor;
long long result = 0;
// 不断减
while(a >= b){
long long c = b;
for(int i = 0;a >= c;++i,c <<= 1){
a -= c;
result += 1 << i;
}//for
}//while
// 正负
if((dividend > 0 && divisor < 0) || (dividend < 0 && divisor > 0)){
result = -result;
}//if
// If it is overflow, return MAX_INT.
if (result > INT_MAX || result < INT_MIN){
result = INT_MAX;
}
return static_cast<int>(result);
}
};

int main(){
Solution solution;
int dividend = -2147483648;
int divisor = -1;
int result = solution.divide(dividend,divisor);
// 输出
cout<<result<<endl;
return 0;
}



2147483648 overflow 所以返回MAX_INT    2147483647

|
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