Leetcode Divide two integers (divide two integers) (*)

Source: Internet
Author: User

translation
不用乘法、除法、取余操作,将两个数相除。如果它溢出了,返回MAX_INT
Original
twowithoutusingandmoditreturn MAX_INT.
Code

I threw myself at the recursion, but I couldn't write it down .... Irritable or find someone else's answer ...

classSolution { Public:intDivideintDividend,intDivisor) {if(!divisor)returnInt_max;if(Divisor = =1)returnDividend;if(Divisor = =-1) {if(Dividend = = int_min)returnInt_max;Else return-dividend; }BOOLS1 = Dividend <0;BOOLS2 = Divisor <0;unsigned intNom = S1? -dividend:dividend;unsigned intden = s2? -divisor:divisor;unsigned intREM =0;unsigned intquot =0; for(inti = to; I >=0; -I.) {REM <<=1; REM |= (nom >> i) &1;if(Rem >= den)                {REM-= den; quot |= (1<< i); }        }returnS1^s2?    -quot:quot; }};

Two more codes ... (Ashamed of ...) )

 Public classsolution{ Public int Divide(intDividend,intDivisor) {//1. Check overflow:2 ways of over flow 1) 0 divisor; 2) Int. minvalue/( -1)        if(Divisor = =0|| Dividend = =int. MinValue && divisor = =-1)return int. MaxValue;//2. Calculate Sign        intSign = dividend >0^ Divisor >0? -1:1, result =0;Longm = Math.Abs ((Long) dividend), n = Math.Abs ((Long) divisor);//3. Looping from 1 to possible maximum POW (2, X) to add into result         while(M >= N) {LongSUBN = n; for(intSubcount =1; M >= subn; Subcount <<=1, Subn <<=1) {m-= subn;            result + = Subcount; }        }returnresult * SIGN; }}
public class solution{PublicintDivide (intDividend,intDivisor) {if(Divisor = =1)returnDividend;if(Dividend = =int. MinValue && divisor = =-1|| divisor = =0)return int. MaxValue;intSign = (Dividend <0) ^ (Divisor <0) ? -1:1, result =0; Long DVD = Math.Abs ((long) dividend), DVS = Math.Abs ((long) divisor); while(DVD >= DVS) {Long Sub = DVS;            intSUBR =1; while(DVD >= ( Sub << 1)) {  Sub <<= 1;SUBR <<=1; } DVD-= Sub;result + = SUBR; }returnSign * result; }}

Copyright NOTICE: This article is nomasp Couvant original article, without permission is prohibited reprint! Welcome to my blog: http://blog.csdn.net/nomasp

Leetcode Divide two integers (divide two 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.