LeetCode, leetcodeoj
Question:
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
Ideas:
It's just like you can purchase a direct ticket for returning home from the next year. If you have a ticket and can solve it with one click, do you have to buy a segmented ticket? In addition, it is easy to make a wrong trip (bugs) during the purchase of the segment ticket, and may not be able to return home (the running result is incorrect ).
package manipulation;public class DivideTwoIntegers { public int divide(int dividend, int divisor) { if ((dividend == Integer.MIN_VALUE && divisor == -1) || divisor == 0) return Integer.MAX_VALUE; if (divisor == 1) return dividend; if (divisor == -1) return -dividend; long a = Math.abs((long)dividend); long b = Math.abs((long)divisor); long base = b; int i = 0; long[] nums = new long[32]; nums[0] = b; while (nums[i] <= a && nums[i] > 0) { nums[i+1] = base << (i+1); ++i; } --i; int cnt = 0; while (a > 0 && i >= 0) { while (a - nums[i] >= 0) { a -= nums[i]; cnt += (1 << i); } --i; } return ((dividend ^ divisor) >> 31) == 0 ? cnt : -cnt; } public static void main(String[] args) { // TODO Auto-generated method stub DivideTwoIntegers d = new DivideTwoIntegers(); System.out.println(d.divide(-2147483648, 2)); }}