【Leetcode】 - Divide Two Integers 位运算实现整数除法
实现两个整数的除法,不许用乘法、除法和求模。题目被贴上了BinarySearch,但我没理解为什么会和BinarySearch有关系。我想的方法也和BS一点关系都没有。
很早以前我就猜想,整数的乘法是不是总是可以用移位和加法来实现?当然可以了,任何整数都可以写成2n或2n+1的形式,移位就是那个乘以2,加法就是最后的+1了嘛。复杂度是O(1),因为整数的移位最多32次,因此在循环中移位的次数也极其有限。
例如123/5:
5 123
<<1
<<1
<<1
<<1
5*16 = 80
5 123-80=43
<<1
<<1
<<1
5*8 = 40
5 43-40=3
5<3, thus result = 16+8 = 24
(if dividend == 125, finally we have:
5==5, thus result = 16+8+1 = 25;)
于是:
int divide(long long a, long long b) {
if(b==) return a>=?0x7fffffff:0x80000000;
if(a==) return ;
if(b==) return a;
int sgn = (a^b) & 0x80000000; //0 -> +, 0x80000000 -> -
if(a<) a=-a;
if(b<) b=-b;
//a,b>0
if(a<b) return ;
int res = , _b = b, twon;
while(a>b)
{
twon = ;
while(a>b)
{
b<<=;
twon<<=;
}
b>>=;
twon>>=;
a -= b;
b = _b;
res += twon;
}
if(a==b) res++;
if(sgn)
return -res;
else return res;
}
Leetcode上测试时间为36ms。
这里玩了一个小trick,用long long而不是int来存变量了,主要是防止被除数太大时除数移位会溢出变成负数。当然去掉long long也不难啦,加上溢出的判断就好了。只不过,这里的溢出判断和函数atoi是有所不同的,atoi中的result每次乘以10再加上0-9,而这边是乘以2。以上。
【Leetcode】 - Divide Two Integers 位运算实现整数除法的更多相关文章
- LeetCode: Divide Two Integers 解题报告
Divide Two Integers Divide two integers without using multiplication, division and mod operator. SOL ...
- Leetcode Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...
- leetcode:single-number-ii(Java位运算)
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- [LeetCode] Divide Two Integers( bit + 二分法 )
Divide two integers without using multiplication, division and mod operator. 常常出现大的负数,无法用abs()转换成正数的 ...
- Leetcode:Divide Two Integers分析和实现
题目要求我们用一个32位整数整除另外一个整数,但是不允许我们使用除法,乘法和取模运算. 有趣的问题,下面说一下我的思路: 首先,先给出两个正整数除法运算的过程.假设a为被除数,而b为除数.在计算机中无 ...
- leetcode Single Number II - 位运算处理数组中的数
题目描述: 给定一个包含n个整数的数组,除了一个数出现一次外所有的整数均出现三次,找出这个只出现一次的整数. 题目来源: http://oj.leetcode.com/problems/single- ...
- [LeetCode] Single Number II 位运算
Given an array of integers, every element appears three times except for one. Find that single one. ...
- leetcode Divide Two Integers python
class Solution(object): def divide(self, dividend, divisor): """ :type dividend: int ...
随机推荐
- HttpContext 讲解
HttpContext类:封装有关个别HTTP请求的所有HTTP特定的信息,又叫上下文.看到这个解释,我觉得有些抽象,Http特定信息具体又是什么?看了下备注:为继承 IHttpModule 和 IH ...
- ASP.NET缓存全解析2:页面输出缓存 转自网络原文作者李天平
页面输出缓存是最为简单的缓存机制,该机制将整个ASP.NET页面内容保存在服务器内存中.当用户请求该页面时,系统从内存中输出相关数据,直到缓存数据过期.在这个过程中,缓存内容直接发送给用户,而不必再次 ...
- mongodb c++ 驱动库编译
git clone 'https://github.com/mongodb/mongo-cxx-driver.git' scons -j2 --c++11=on --sharedclient --us ...
- C语言知识总结(2)
选择结构-if if(表达式) {} {}为作用域 多重if-else 例如: #include <stdio.h> int main(){ ; ){ printf("没有购物 ...
- 页面传值总结Block
// AppDelegate.m // 页面传值总结 // // Created by qianfeng on 15/6/13. // Copyright (c) 2015年 qianfeng. Al ...
- UI1_UIButton
// // AppDelegate.m // UI1_UIButton // // Created by zhangxueming on 15/6/30. // Copyright (c) 2015年 ...
- 大数求模 sicily 1020
Search
- 随便写了一个DAO
package com.java; public class ExamStudent { /** * 流水号 */ private int flowId; /** * 四级.六级 */ private ...
- 6.struts登陆页面的演示
1.创建一个web project "Struts_1" 添加struts的jar包 --在项目文件右键->myeclipse->add struts... ...
- nginx配置多个网址
实战Nginx与PHP(FastCGI)的安装.配置与优化:http://ixdba.blog.51cto.com/2895551/806622 Nginx配置文件详细说明:http://www.cn ...