Leetcode|Divide Two Integers
来源:互联网 发布:如何下载windows 10 编辑:程序博客网 时间:2024/06/06 04:26
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
位操作,详见我的另一篇讲解位操作的博文。
此题在leetcode的通过率只有15%,主要是溢出的问题需要好好考虑。
int divide(int x,int y){ assert(y!=0); bool neg=(x<0)^(y<0); unsigned int x1=x>0?x:-x; unsigned int y1=y>0?y:-y; long long pos=y1; unsigned int res=0; int bit=0; for(;pos<=x1;bit++) { pos=pos<<1; } //if(x1>INT_MAX) return bit; while(x1>=y1){ if(x1>=pos){ res|=1<<bit; x1-=pos; }else{ pos>>=1; bit--; } } if(res>INT_MAX&&!neg) return INT_MAX; if(neg) res=-res; return (int)res;}
0 0
- LeetCode: Divide Two Integers
- [LeetCode]Divide Two Integers
- LeetCode: Divide Two Integers
- [LeetCode] Divide Two Integers
- [Leetcode] Divide Two Integers
- LeetCode Divide Two Integers
- 【leetcode】Divide Two Integers
- [LeetCode]Divide Two Integers
- LeetCode-Divide Two Integers
- LeetCode: Divide Two Integers
- Divide Two Integers - leetcode
- Leetcode: Divide Two Integers
- LeetCode:Divide Two Integers
- leetcode Divide Two Integers
- LeetCode Divide Two Integers
- LeetCode | Divide Two Integers
- Leetcode: Divide Two Integers
- Divide Two Integers -- LeetCode
- start_kernel——setup_nr_cpu_ids
- 计算器小例子
- 【单调栈】POJ 2559 Largest Rectangle in a Histogram
- Linux系统调用列表
- 87.计算n!
- Leetcode|Divide Two Integers
- IOS沙盒详解
- HBase学习总结(3):HBase的数据模型及工作机制
- linux下mysql安装
- Codility-task 1-Tape Equilibrium
- LR vs LWLR
- nodejs之SVG转图片下载方案
- 输入一行字符,统计有多少个单词
- HDU 3681 Prison Break(bfs+二分+状压DP)