leetcode 29 -- Divide Two Integers
来源:互联网 发布:淘宝卖啥好? 编辑:程序博客网 时间:2024/05/22 11:31
Divide Two Integers
感想:
这道题因为有一点小问题一直导致AC不过,一会我会先贴上自己的正确代码,之前错误的贴到后面。
在一直解决不了遇到的问题时,我去网上搜了下别人做这道题的思路,感觉自己的思路也算比较简单吧,就一直坚持自己的思路,依然不知道哪里错了,调试于是查了查abs函数,发现了还有其他的类abs函数,自己没用过,labs和llabs,于是发现了自己边界的问题,改正加上了一个处理特殊情况的if句子,AC了。
我想起了我当时看得别人的思路和代码时并没有用llabs和处理特殊情况,于是又查了他们的然后粘贴到leetcode上,选了第一页人气高
的4个都没有AC,都显示错误,而且他们也没有说明。
如果是leetcode更新了,那么就当我说废话吧。
如果不是,就不要写错误的代码给别人看,首先像我们这种小白看到人气稍高的博客是很相信的,会感谢你们写了这篇博客且提供思路,但如果是错误的…
好了,废话多了…
题目:
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
题意:
不用乘法和除法,以及取余,实现两个数的除法。如果溢出返回MAX_INT
思路:
我用的是通过不断累加来靠近结果,直到取得最终值。
因为一般都能想到用减法来实现除法,不过肯定是超时的,如果一个为很大值另外一个为1,但我也是用类似思路,不过以指数来靠近就会快很多了,首先让除数以2倍关系递增,然后直到刚好超过被除数,在递增过程中保存除数递增的每个结果到一个数组里,并且保存刚好超过时的数组下标Max,数组的下标i就代表着这是2的i次方个除数,那么从遍历Max-0,累加,如果累加中某一个和值大于了被除数,不加这个数,继续下一个,不大于则加上,为了无限靠近。因为我们都知道2^1 + 2^2 + … + 2^(n-1) = 2^n-1这个公式。累加过程中我们要同时累加数组下标代表的除数个数。最后返回即可。
代码(正确):
class Solution {public: int divide(int dividend, int divisor) { //特殊情况,必须指出,因为两个转换为正数相除不会得到返回的结果。会溢出 if(dividend == -2147483648 && divisor == -1){ return 2147483647; } //辅助数组,也必须为long long static long long int array[40]; //因为参数可能为正或为负,必须转换为整数,用long long是为了避免溢出,重点:用long long 是必须用llabs,否则不会得到正确结果。 long long a = llabs((long long)dividend); long long b = llabs((long long)divisor); int ret = 0; long long sum = 0; int i = -1; //指数逼近,并保存到数组 while(b <= a){ array[++i] = b; b <<= 1; } //0-Max,if-else为思路里面的判断,大于则不加,否则加上,并且给返回值相应的除数个数。 for(int j = 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{ return ret; } }};
代码(错误):
class Solution {public: int divide(int dividend, int divisor) { //错误 int型且没判断特殊情况 static int array[40]; //错误 abs long long a = abs((long long)dividend); long long b = abs((long long)divisor); int ret = 0; int sum = 0; int i = -1; while(b <= a){ array[++i] = b; b <<= 1; } for(int j = 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{ return ret; } }};
错误代码仅仅为了提醒自己和看到博客的人。
最后贴上
还是希望大家都AC ^_^
- leetcode 29: Divide Two Integers
- LeetCode(29)Divide Two Integers
- [leetcode 29] Divide Two Integers
- [leetcode] 29 Divide Two Integers
- leetcode 29 Divide Two Integers
- Leetcode 29 Divide Two Integers
- LeetCode 29 - Divide Two Integers
- leetcode 29 -- Divide Two Integers
- leetcode 29:Divide Two Integers
- leetcode 29: Divide Two Integers
- Leetcode#29 Divide Two Integers
- LeetCode(29)Divide Two Integers
- LeetCode-29 Divide Two Integers
- Leetcode#29||Divide Two Integers
- leetcode-29 Divide Two Integers
- leetcode 29:Divide Two Integers
- leetcode 29:Divide Two Integers
- LeetCode 29: Divide Two Integers
- HTTP之Range理解
- [049] 微信公众平台开发视频公开课第1讲-基础入门
- 在myeclipse中运行jsp弹出debug怎么解决
- SAT数学强化练习题及解析
- spring配置文件详解
- leetcode 29 -- Divide Two Integers
- 免费开源API
- python:tkinter弹出对话款,并获取输入框的值
- 【已解决】Python脚本运行出现语法错误:IndentationError: unindent does not match any outer indentation level
- BootDev CEO Keithyau 受邀作为讲师 AWS线下沙龙—云计算环境下的可扩展性架构(杭州站)
- sortUsingSelector使用方法,以及原理
- boost学习--thread
- 使用facebook infer寻找项目潜在BUG
- 雅虎网站优化34条法则