package leetcode;

/**
 * https://oj.leetcode.com/problems/divide-two-integers/
 */
public class Problem29 {
    public int divide(int dividend, int divisor) {
        long result = 0;
        boolean negative = false;
        if ((dividend < 0 && divisor > 0)) {
            negative = true;
            dividend = ~dividend + 1;
        } else if (dividend > 0 && divisor < 0) {
            negative = true;
            divisor = ~divisor + 1;
        } else if (dividend < 0 && divisor < 0) {
            dividend = ~dividend + 1;
            divisor = ~divisor + 1;
        }
        long tmp = dividend;
        while (tmp > 0) {
            tmp -= divisor;
            result++;
        }
        result = (negative) ? ~result + 1 : result;
        if (result >= Integer.MAX_VALUE) {
            return Integer.MAX_VALUE;
        } else if (result <= Integer.MIN_VALUE) {
            return Integer.MIN_VALUE;
        }
        return (int) result;
    }
    
    public static void main(String[] args) {
        Problem29 prob = new Problem29();
        System.out.println(prob.divide(8, 2));
        System.out.println(prob.divide(100, 2));
        System.out.println(prob.divide(50, 5));
        System.out.println(prob.divide(250, 50));
        System.out.println(prob.divide(-250, -50));
        System.out.println(prob.divide(-250, 50));
        System.out.println(prob.divide(250, -50));
        System.out.println(prob.divide(2, 3));
        System.out.println(prob.divide(2147483647, 2));
    }
}

@@ -178,16 +178,90 @@
 lt = 0;%0A
+        long longDividend = dividend;%0A        long longDivisor = divisor;%0A
         
@@ -299,17 +299,21 @@
    if ((
-d
+longD
 ividend 
@@ -315,25 +315,29 @@
 dend %3C 0 && 
-d
+longD
 ivisor %3E 0))
@@ -372,33 +372,37 @@
 ue;%0A            
-d
+longD
 ividend = ~divid
@@ -388,33 +388,37 @@
 longDividend = ~
-d
+longD
 ividend + 1;%0A   
@@ -425,33 +425,37 @@
      %7D else if (
-d
+longD
 ividend %3E 0 && d
@@ -449,25 +449,29 @@
 dend %3E 0 && 
-d
+longD
 ivisor %3C 0) 
@@ -505,33 +505,37 @@
 ue;%0A            
-d
+longD
 ivisor = ~diviso
@@ -520,33 +520,37 @@
  longDivisor = ~
-d
+longD
 ivisor + 1;%0A    
@@ -564,17 +564,21 @@
 lse if (
-d
+longD
 ividend 
@@ -584,17 +584,21 @@
  %3C 0 && 
-d
+longD
 ivisor %3C
@@ -615,17 +615,21 @@
         
-d
+longD
 ividend 
@@ -631,17 +631,21 @@
 dend = ~
-d
+longD
 ividend 
@@ -661,17 +661,21 @@
         
-d
+longD
 ivisor =
@@ -676,17 +676,21 @@
 isor = ~
-d
+longD
 ivisor +
@@ -723,74 +723,486 @@
  tmp
- = dividend;%0A        while (tmp %3E 0) %7B%0A            tmp -= divisor;
+Dividend = longDividend;%0A        while (tmpDividend %3E longDivisor) %7B%0A            long tmpDivisor = longDivisor;%0A            long tmpResult = 1;%0A            while (tmpDividend %3E tmpDivisor) %7B%0A                tmpDivisor = tmpDivisor %3C%3C 1;%0A                tmpResult %3C%3C= 1;%0A            %7D%0A            tmpResult %3E%3E= 1;%0A            result += tmpResult;%0A            tmpDivisor = tmpDivisor %3E%3E 1;%0A            tmpDividend -= tmpDivisor;%0A        %7D%0A        if (tmpDividend == longDivisor) %7B
 %0A   
@@ -1220,10 +1220,13 @@
 sult
-++
+ += 1
 ;%0A  
@@ -1225,32 +1225,41 @@
 += 1;%0A        %7D%0A
+        %0A
         result =
@@ -1512,555 +1512,6 @@
   %7D%0A
-    %0A    public static void main(String%5B%5D args) %7B%0A        Problem29 prob = new Problem29();%0A        System.out.println(prob.divide(8, 2));%0A        System.out.println(prob.divide(100, 2));%0A        System.out.println(prob.divide(50, 5));%0A        System.out.println(prob.divide(250, 50));%0A        System.out.println(prob.divide(-250, -50));%0A        System.out.println(prob.divide(-250, 50));%0A        System.out.println(prob.divide(250, -50));%0A        System.out.println(prob.divide(2, 3));%0A        System.out.println(prob.divide(2147483647, 2));%0A    %7D%0A
 %7D%0A

Solve problem 29 (finally!)