LeetCode-Divdend two Integers
题目:
Divide two integers without using multiplication, division and mod operator.
思路分析
二分法.将除数不断增倍,而结果同样扩大两倍,直到除数的值大于被除数.然后再利用被除数减去除数最后增长到小于被除数的值,递归求出结果.
例如:123/4
4<123 4*2=8<123 8*2=16>123 16*2=32<123
32*2=64<123 64*2=128>123 故结果增长的值为64/4=16.
再利用123-64=59,再次递归求出结果,最后肯定能得出59/4=14,余数为3,3<4抛弃.
故最终值为16+14=30
代码:
public Solution{
public:
long long interDivide(unsigned long long dividend,
unsigned long long divisor){
if(dividend<divisor) return ;
long long result=;
unsigned long long tmp=divisor,left;
while(tmp<=dividend){
left=dividend-tmp;
tmp<<=;
if(tmp>dividend){
break;
}
else{
result<<=;
}
}
return result+interDivide(left,divisor);
}
int Divide(int dividend,int divisor){
unsigned long long _dividend=abs((long long)dividend);
unsigned long long _divisor=abs((long long)divisor);
bool positive=((dividend>=) && (divisor>)) || ((dividend<=) && (divisor<));
return positive?interDivide(_dividend,_divisor):(-)*interDivide(_dividend,_divisor);
}
};
之所以用unsigned long long是为了防止溢出.
LeetCode-Divdend two Integers的更多相关文章
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- LeetCode: Divide Two Integers 解题报告
Divide Two Integers Divide two integers without using multiplication, division and mod operator. SOL ...
- LeetCode 970. Powerful Integers (强整数)
题目标签:HashMap 题目让我们找出所有独一的powerful integers 小于bound的情况下. 把 x^i 看作 a:把 y^j 看作b, 代入for loop,把所有的情况都遍历一遍 ...
- Leetcode Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...
- [LeetCode] Divide Two Integers( bit + 二分法 )
Divide two integers without using multiplication, division and mod operator. 常常出现大的负数,无法用abs()转换成正数的 ...
- Leetcode:Divide Two Integers分析和实现
题目要求我们用一个32位整数整除另外一个整数,但是不允许我们使用除法,乘法和取模运算. 有趣的问题,下面说一下我的思路: 首先,先给出两个正整数除法运算的过程.假设a为被除数,而b为除数.在计算机中无 ...
- LeetCode OJ-- Divide Two Integers *
https://oj.leetcode.com/problems/divide-two-integers/ 在不使用乘法.除法.求余的情况下计算除法. 使用减法计算,看看减几次. 刚开始寻思朴素的暴力 ...
- 【LeetCode】Powerful Integers(强整数)
这道题是LeetCode里的第970道题. 题目描述: 给定两个正整数 x 和 y,如果某一整数等于 x^i + y^j,其中整数 i >= 0 且 j >= 0,那么我们认为该整数是一个 ...
- leetcode Divide Two Integers python
class Solution(object): def divide(self, dividend, divisor): """ :type dividend: int ...
- leetcode 29-> Divide Two Integers without using multiplication, division and mod operator
class Solution(object): def divide(self, dividend, divisor): """ :type dividend: int ...
随机推荐
- mysql定时执行及延时执行,实现类似sql server waitfor功能
熟悉SQL Server的人都知道,它有一个很有用的功能,waitfor time和waitfor delay,前者表示在某个时间执行,后者表示等待多长时间执行.在我们测试功能和定时执行的时候特别有用 ...
- 数学函数类方法的使用.java
public class Test { public static void main(String[] args) { double a=2,b=3; double z1=Math.pow(a,b) ...
- php echo字符串的连接格式
echo "<td align=\"center\"><img src=\""; 1. \" \" 2. ...
- phpmyadmin自增字段
自增字段必须为primary key 2种方法: 1- ALTER TABLE `qr_role` CHANGE `ROLE_ID` `ROLE_ID` INT(11) NOT NULL AUTO_I ...
- jquery1.9学习笔记 之选择器(基本元素三)
标签选择器("element") 描述: 选择所有与给出标签名相匹配的元素. 同功能的JS原生方法:getElementByTagName() 例子: 查找每个div元素. &l ...
- Flask学习记录之Flask-Migrate
一.配置Flask-Migrate from flask.ext.migrate import Migrate, MigrateCommand migrate = Migrate(app,db) #第 ...
- gcc-gdb
1.使用gdb ,习惯了cgdb ,加上了vim. 调试发现,使用了stl,不知如何查看使用的vector,stack等中的值,来吧.gdb-std-views.下载 1)下载好之后,mv stl-v ...
- iptables 顺序
-A INPUT -s 115.236.6.6/32 -p udp -m udp --dport 111 -j ACCEPT -A INPUT -s 10.175.197.98/32 -p udp - ...
- Windows Azure 存储管理器 (2014)
Windows Azure存储用户经常希望能够在"管理器"中查看他们的数据,管理器指的是一款可用于显示存储帐户数据的工具.我们之前提供了我们所知的存储管理器列表.在本文中,我 ...
- bzoj1151
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1151 状压DP,枚举前面4个,使得环型变线型. #include<cstdio> ...