-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode29.cpp
95 lines (90 loc) · 2.25 KB
/
leetcode29.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*************************************************
Author: wenhaofang
Date: 2021-05-11
Description: leetcode29 - Divide Two Integers
*************************************************/
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <queue>
#include <algorithm>
#include <math.h>
using std::vector;
using std::string;
using std::unordered_map;
using std::unordered_set;
using std::stack;
using std::queue;
using std::priority_queue;
using std::max;
using std::min;
using std::pair;
using std::cin;
using std::cout;
using std::endl;
/**
* 方法一:倍增
*
* 实际时间复杂度:Runtime: 0 ms, faster than 100.00% of C++ online submissions
* 实际空间复杂度:Memory Usage: 5.9 MB, less than 22.33% of C++ online submissions
*/
class Solution {
public:
int divide(int dividend, int divisor) {
// 特判
if (dividend == 0) {
return 0;
} else if (divisor == 1) {
return dividend;
} else if (divisor == -1) {
return dividend == INT_MIN ? INT_MAX : -dividend;
} else if (dividend == 1 || dividend == -1) {
return 0;
}
// 符号
int sign = 1;
if (dividend < 0) {
sign = -sign;
} else {
dividend = -dividend;
}
if (divisor < 0) {
sign = -sign;
} else {
divisor = -divisor;
}
// 数字
int ans = div(dividend, divisor);
// 返回
return sign * ans;
}
// 计算两个负数间的除法(递归)
int div(int dividend, int divisor) {
if (dividend > divisor) {
return 0;
}
int ans = 1;
int cur = divisor;
while (
INT_MIN - cur <= cur &&
cur + cur >= dividend
) {
cur += cur;
ans += ans;
}
return ans + div(dividend - cur, divisor);
}
};
/**
* 测试
*/
int main() {
Solution* solution = new Solution();
int dividend = 7;
int divisor = -3;
int ans = solution->divide(dividend, divisor);
cout << ans << endl;
}