Leetcode-Divide-integers

Source: Internet
Author: User

Divide integers

Feelings:
The problem has been caused by a little bit of trouble. AC However, I will first put my own correct code, before the wrong post to the back.
But I have been on the way to solve the problem I encountered, I went to the online search for someone else to do the idea of the problem, with Baidu (do not spit groove-), also probably looked at the next, feel their thinking is relatively simple bar, has been insisting on their own ideas, still do not know where the wrong, Later nap sleep will head sober down and looked at the problem, debugging then checked the ABS function, found there are other classes abs function, their own useless, labs and llabs, so found their own boundary problem, corrected with a special case of the if sentence, AC. That's not the point.
The point is, I think of me at that time Baidu out of other people's ideas and code when not with Llabs and handling special cases, and then checked their and then pasted to Leetcode, selected the first page 人气高 of the 4 are not AC, all show errors, and they did not explain.
If it's a leetcode update, then let me talk nonsense.
If not, do not write the wrong code to others, first, like our small white to see a slightly higher popularity of the blog is very confident that Daniel, will thank you for writing this blog and provide ideas, but if it is wrong, it will be a waste of our time and feelings.
All right, that's a lot of crap.

Topic:
Divide-integers without using multiplication, division and mod operator.
If It is overflow, return max_int.

Test instructions
Without penalty and division, as well as taking the remainder, achieve two number of divisions. If overflow returns max_int

Ideas:
I used to get close to the result by accumulating it until I got the final value.
Because the general can think of using subtraction to achieve division, but it must be timed out, if one is a large value of the other one is 1, but I also use a similar idea, but the index to close to a lot faster, first let the divisor in twice times the relationship increment, and then until just over dividend, In the increment process to save the divisor increment of each result into an array, and save just over the array subscript max, the array of subscript I represents this is a 2 I divisor, then from the traversal Max-0, accumulate, if the sum of one and the value is greater than the divisor, do not add this number, continue the next, Not greater than then plus, in order to infinity near. Because we all know 2^1 + 2^2 + ... + 2^ (n-1) = 2^n-1 this formula. In addition, we want to add the number of the array subscript. Finally, you can return.

code (correct):
classSolution { Public:intDivideintDividend,intDivisor) {///special case, it must be noted that because two conversions to a positive number divide will not get the result returned. Will overflow        if(Dividend = =-2147483648&& divisor = =-1){return 2147483647; }//auxiliary array, also must be a long long        Static Long Long int Array[ +];//Because the parameter may be positive or negative, it must be converted to an integer, with long long to avoid overflow, focus: a long long is required to use llabs, otherwise it will not get the correct result.         Long LongA = Llabs ((Long Long) dividend);Long Longb = Llabs ((Long Long) divisor);intRET =0;Long Longsum =0;inti =-1;//exponential approximation, and save to array         while(b <= a) {Array[++i] = b; b <<=1; }//0-max,if-else for the idea inside the judgment, greater than not add, otherwise plus, and give the return value corresponding to the addition of several numbers.          for(intj = i; J >=0; j--) {if(sum+Array[j] > a) {Continue; }Else{Sum + =Array[j]; RET + =POW(2, j); }        }//Because we have converted all parameters to positive numbers, we want to determine the correct symbol for the return value at this point.         if((Dividend <0&& divisor >0) || (Dividend >0&& Divisor <0)){return-ret; }Else{returnRet }    }};
Code (Error):
classSolution { Public:intDivideintDividend,intDivisor) {//Error int type without judging special case        Static int Array[ +];//Error ABS        Long LongA =ABS((Long Long) dividend);Long Longb =ABS((Long Long) divisor);intRET =0;intsum =0;inti =-1; while(b <= a) {Array[++i] = b; b <<=1; } for(intj = i; J >=0; j--) {if(sum+Array[j] > a) {Continue; }Else{Sum + =Array[j]; RET + =POW(2, j); }        }if((Dividend <0&& divisor >0) || (Dividend >0&& Divisor <0)){return-ret; }Else{returnRet }    }};

the error code is just to remind yourself and the person who sees the blog.
Finally put on

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.