[Leetcode] Divide integers

Source: Internet
Author: User

Divide integers divide, integers without using multiplication, Division and mod Operator. if it is OVERFL ow, return max_int.  idea: similar to binary search algorithm, but we do is to multiply or subtract the divisor. Note: 1, in advance to determine the divisor and the divisor of positive and negative, used to determine the positive and negative results of the final result. 2, in order to prevent overflow, the int type is converted to a long long type for operation, and the result is converted to int type. For example:100/3  the first step: because of the > 3, get dividend = 100-3 = 6, Dividor = 3 + 3 = 1, Pre_ret = 1, ret =; the second step: because of the > 6, To dividend = 97-6 = Dividend, Dividor = 6 + 6 = N, Pre_ret = 1 + 1 = 2, ret = 1 + 2 = 3; step three: Due to the > 12, the Ividor = 24, Pre_ret = 4, ret = 7; Fourth step: Because the >, get dividend =, Dividor =, Pre_ret = 8, ret = 15; Fifth step: Because of > 48 , Get dividend = 7, Dividor = 7, Pre_ret = +, ret = 31;  (because at this time dividend > Dividor, so the multiplication of dividor here ends, the next do the double minus) sixth step: Because < 96, Get dividend = 7, Dividor =, Pre_ret = 8, ret = 31; Fourth Step: Because 7 < 48, get dividend = 7, Dividor =, Pre_ret = 4, ret = 31; Fourth Step: Because 7 < 24, get dividend = 7, Dividor =, Pre_ret = 2, ret = 31, Fourth step: Because 7 < 12, get dividend = 7, Dividor = 6, Pre_ret = 1, ret = 31; Fourth step: Because 7 > 6, get dividend = 1, Dividor = 3, Pre_ret = 0, ret = 33; At this time 1 is less than the most primitive divisor 3, terminating the loop, and returning RET according to the positive and negative relationship of divisor and divisor Values.  
1 classSolution2 {3  Public:4   intDivideintDividend,intDividor)5   {6     if(Dividend = = Int_min && Dividor = =-1)7       returnInt_max;8  9Auto sign = [=] (intx)int{returnX>0?1: -1;};Ten     intP1 =Sign (dividend); One     intP2 =Sign (Dividor); A   -     Long LongN1 = ABS (static_cast<Long Long>(dividend)); -     Long LongN2 = ABS (static_cast<Long Long>(Dividor)); the   -     Long LongRET =0; -     Long LongPre_ret =0; -      while(N1 >=n2) +     { -       if(ret = =0&& Pre_ret = =0) +       { APre_ret =1; atRET =1; -       } -       Else -       { -Pre_ret + =Pre_ret; -RET = Pre_ret +ret; in       } -   toN1-=N2; +N2 + =N2; -     } the   *      while(N2 >= Abs (static_cast<Long Long> (Dividor)) && N1 >= ABS (static_cast<Long Long>(Dividor))) $     {Panax Notoginseng       if(Pre_ret = =0) -       { theRET + =1; +          Break; A       } the   +       if(N1 <n2) -       { $N2 = N2 >>1; $         if(N1 = =n2) -         { -RET + =Pre_ret; the            Break; -         }WuyiPre_ret = Pre_ret >>1; the       } -       Else Wu       { -N1-=N2; AboutN2 = N2 >>1; $         if(N1 = =n2) -         { -RET + =Pre_ret; -            Break; A         } +ret = Pre_ret + Pre_ret +ret; thePre_ret = Pre_ret >>1; -       } $     } the   the     if(P1 = =p2) the       returnret; the     Else -       return-ret; in   } the};

[Leetcode] 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.