Leetcode notes: Divide, integers

Source: Internet
Author: User

I. Title Description

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

If It is overflow, return max_int.

Two. Topic analysis

The meaning of the topic is simple and straightforward, which requires not using multiplication, division, and remainder mods, entering two integers, and outputting the result of the division operation.

Go out multiplication method, the remaining only add subtraction and bit operation, it is not difficult to think of, and directly using subtraction, dividend minus the divisor size of the value, the record is reduced number of times, it is certainly possible to get the same result as division operation, but the method compared to a fool, and will time out, the complexity of O (n).

The complexity of O (LOGN) can be achieved by using bit operations, but it is not so easy to think of a specific operation, first of all, we know that any integer can be represented as a set of linear combinations of bases based on a power of 2, num = flag0 * 2^0 + flag1 * 2^1 + flag2 * 2^2 + ... + flagn * 2^n where the flag0, flag1, flag2, ..., flagn value is 0 & 1 .

Based on the above facts, if the order is: dividend / divisor = num

dividend = divisor * num = divisor * (flag0 * 2^0 + flag1 * 2^1 + flag2 * 2^2 + ... + flagn * 2^n)

For divisor, use the shift operation << make it double each time, thus reducing the number of times to seek quotient. Here are the steps:

    1. When the divisor is greater than the divisor, the divisor is multiplied by 2 (the code uses the variable step to record each divisor by 2) until step is greater than dividend. Record the number of shift operations I.
    2. If the dividend is greater than the divisor, then dividend minus step. Until the divisor is less than the divisor. Save the results.
    3. Output result.

Note:

byte:128~127 (1Byte)short :32768~32767 (2Bytes)int:-2147483648~2147483647 (4Bytes)long:-9223372036854774808~9223372036854774807 (8Bytes)

Three. Sample code

classSolution { Public:intDivideintDividend,intDivisor) {if(Dividend = =0|| divisor = =0)return 0;if(Dividend = = Int_min && divisor = =-1)returnInt_max;//Overflow        BOOLNegative = (Dividend >0&& Divisor <0) || (Dividend <0&& divisor >0);LongPositivedividend =ABS(Long(dividend));LongPositivedivisor =ABS(Long(divisor));Longresult =0; while(Positivedividend >= Positivedivisor)//dividend greater than divisor{LongStep = positivedivisor; for(inti =0; Positivedividend >= Step; ++i, Step <<=1) {positivedividend = Positivedividend-step; Result + =1<< i; }        }returnNegative?    -result:result; }};

Four. Summary

Using bit arithmetic to solve this kind of problem is quite easy to think about, but how to do it is another matter.

Leetcode notes: Divide, integers

Contact Us

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.