-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDivisor.java
More file actions
38 lines (26 loc) · 767 Bytes
/
Divisor.java
File metadata and controls
38 lines (26 loc) · 767 Bytes
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
//https://leetcode.com/problems/divide-two-integers/
public class Divisor {
public static void main(String[] args) {
System.out.println(divide(Integer.MIN_VALUE, -1));
System.out.println(divide(Integer.MAX_VALUE, 1));
System.out.println(divide(7, -3));
}
public static int divide(int dividend, int divisor) {
long a = 1;
long n = dividend;
long m = divisor;
if (n*m < 0)
a = -1;
if (n < 0)
n = -n;
if (m < 0)
m = -m;
long quot = n/m;
quot = a*quot;
if (quot > Integer.MAX_VALUE)
return Integer.MAX_VALUE;
if (quot < Integer.MIN_VALUE)
return Integer.MIN_VALUE;
return (int) quot;
}
}