MCPcopy Create free account
hub / github.com/Blankj/awesome-java-leetcode / divide

Method divide

src/com/blankj/medium/_0029/Solution.java:12–29  ·  view source on GitHub ↗
(int dividend, int divisor)

Source from the content-addressed store, hash-verified

10 */
11public class Solution {
12 public int divide(int dividend, int divisor) {
13 if (dividend == Integer.MIN_VALUE && divisor == -1) {
14 return Integer.MAX_VALUE;
15 }
16 long dvd = Math.abs((long) dividend);
17 long dvr = Math.abs((long) divisor);
18 int res = 0;
19 while (dvd >= dvr) {
20 long temp = dvr, multiple = 1;
21 while (dvd >= temp << 1) {
22 temp <<= 1;
23 multiple <<= 1;
24 }
25 dvd -= temp;
26 res += multiple;
27 }
28 return (dividend < 0) ^ (divisor < 0) ? -res : res;
29 }
30
31 public static void main(String[] args) {
32 Solution solution = new Solution();

Callers 1

mainMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected