59. Divide Two Integers
- Divide Two Integers My Submissions QuestionEditorial Solution
Total Accepted: 66073 Total Submissions: 421509 Difficulty: Medium
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
思路:每次减去b(同时b不断的翻倍,从1,2,2^2…)
为什么这样是可行的,因为如果a=nb+x
那么n作为一个整数一定是可以表示成 2m+2m−1+...+21+20
所以从20,21。。。依次减是可行的
#define IMAX numeric_limits<int>::max()
#define IMIN numeric_limits<int>::min()
class Solution {
public:
int divide(int dividend,int divisor) {
assert(divisor!=0);
int sign = (dividend<0&&divisor>0||dividend>0&&divisor<0)?-1:1;
unsigned int a = dividend<0?-dividend:dividend;
unsigned int b = divisor<0?-divisor:divisor;
unsigned int c = b;
int multi=0;
while(a>=b){
int i=1;
while(a>=b){
a -=b;
multi+=i;
if(b<IMAX>>1){
b=b<<1;
i<<=1;
}
}
b=c;
}
if(sign>0&&multi==IMIN)return IMAX;
else return multi*sign;
}
};
59. Divide 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 without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...
- leetcode-【中等题】Divide Two Integers
题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...
- [LintCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 62. Divide Two Integers
Divide Two Integers Divide two integers without using multiplication, division and mod operator. 思路: ...
- Divide Two Integers leetcode
题目:Divide Two Integers Divide two integers without using multiplication, division and mod operator. ...
- Java for LeetCode 029 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()转换成正数的 ...
- LeetCode29 Divide Two Integers
题目: Divide two integers without using multiplication, division and mod operator. If it is overflow, ...
随机推荐
- [no code][scrum meeting] Alpha 4
项目 内容 会议时间 2020-04-09 会议主题 OCR相关的技术展示与讨论 会议时长 30min 参会人员 全体成员 $( "#cnblogs_post_body" ).ca ...
- AFO记
希望永远也不要动笔写这个. 发以自勉
- 六个好习惯让你的PCB设计更优(转)
PCB layout工程师每天对着板子成千上万条走线,各种各样的封装,重复着拉线的工作,也许很多人会觉得是很枯燥无聊的工作内容.看似软件操作搬运工,其实设计人员在过程中要在各种设计规则之间做取舍,兼顾 ...
- 洛谷 P5658 [CSP-S2019] 括号树
链接: P5658 分析: 显然我们应该在dfs树的同时维护每个点的答案. 注意到第 \(u\) 个点的答案可以分成两部分,不包含 \(u\) 点时的答案,和加入 \(u\) 点后新增的答案,前者可以 ...
- 注意 .NET string.GetHashCode() 用法
需求案例:需要把字符串存入数据库,并且要求数据库中不能有重复的字符串,由此就引出了将字符串hash成特定的hash值,依靠查询hash值是否重复来判断字符串是否重复.这样做的好处在于查询重复字符串的代 ...
- python pip whl安装和使用
转载:https://www.cnblogs.com/klb561/p/9271322.html 1 python的安装 首先,从python的官方网站 www.python.org下载需要的pyth ...
- cf20B Equation(认真仔细题)
题意: 求AX^2+BX+C=0的根 思路: 考虑到A,B,C所有可能的情况 代码: double a,b,c; int main(){ cin>>a>>b>>c; ...
- Oracle 19c 没有匹配的协议
Oracle12c连接问题ORA-28040:没有匹配的验证协议 造成改问题的原因是客户端版本太低.修改sqlnet.ora文件可以让服务器适配低版本的客户端 sqlnet.ora文件中加入 SQLN ...
- makefile简单学习(一)
第一层 显式规则 目标:依赖 [tab] 指令 伪目标:.PHONY: 递归规则 hello : hello.o gcc hello.o -o hello hello.o : hello.s gcc ...
- 【python】使用python十分钟创建个人聊天机器人教程
以青云客和图灵机器人接口示范python创建个人聊天机器人教程 一.以青云客聊天机器人为例示范get请求 官方网址:http://api.qingyunke.com/ 1.接入指引 请求地址 http ...