# [LeetCode] Divide Two Integers

Source: Internet
Author: User

[LeetCode] Divide Two Integers

Divide two integers without using multiplication, division and mod operator.

If it is overflow, return MAX_INT.

Time Complexity log (n)

There is a coner case

Dividend =-2147483648

Divisor =-1;

Dividend =-2147483648

Divisor = 1

The answer is-2147483648 if the positive and negative judgments are placed at the end, overflow

`Public class Solution {public int divide (int dividend, int divisor) {long a = dividend> 0? Dividend:-(long) dividend; long B = divisor> 0? Divisor:-(long) divisor; int sgn = (dividend> 0 & divisor> 0) | (dividend <0 & divisor <0 ))? 1:-1); int res = 0; while (a> = B) {long c = B; int I = 1; while (a> = c) {a-= c; if (a> = 0) {res + = sgn * (Math. pow (2, I-1);} else {break;} c <= 1; I ++ ;}} return res ;}}`

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.