Leetcode Divide Two Integers
Divide two integers without using multiplication, division and mod operator.
不用乘、除、求余操作,返回两整数相除的结果,结果也是整数。
假设除数是2,相除的商就是被除数二进制表示向右移动一位。
假设被除数是a,除数是b,因为不知道a除以b的商,所以只能从b,2b,4b,8b.......这种序列一个个尝试
从a扣除那些尝试的值。
如果a大于序列的数,那么a扣除该值,并且最终结果是商加上对应的二进制位为1的数,然后尝试序列的下一个数。
如果a小于序列的数,那么从头再来,直至a小于b
class Solution {
public:
int divide(int dividend, int divisor) {
long long a = llabs((long long)dividend), b = llabs((long long)(divisor)), result = ;
while(a>=b){
long long c = b;
int i = ;
while(a>=c){
a-=c;
result+=<<i; ++i;
c = c<<;
}
}
if((dividend > && divisor < ) || (dividend < && divisor > )) result = -result;
return (int)result;
}
};
Leetcode 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 Divide two integers without using multiplication, division and mod operator. SOL ...
- [LeetCode] Divide Two Integers( bit + 二分法 )
Divide two integers without using multiplication, division and mod operator. 常常出现大的负数,无法用abs()转换成正数的 ...
- Leetcode:Divide Two Integers分析和实现
题目要求我们用一个32位整数整除另外一个整数,但是不允许我们使用除法,乘法和取模运算. 有趣的问题,下面说一下我的思路: 首先,先给出两个正整数除法运算的过程.假设a为被除数,而b为除数.在计算机中无 ...
- leetcode Divide Two Integers python
class Solution(object): def divide(self, dividend, divisor): """ :type dividend: int ...
- leetcode面试准备:Divide Two Integers
leetcode面试准备:Divide Two Integers 1 题目 Divide two integers without using multiplication, division and ...
- [Leetcode][Python]29: Divide Two Integers
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...
- 【一天一道LeetCode】#29. Divide Two Integers
一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...
- 【Leetcode】【Medium】Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
随机推荐
- Python学习笔记——列表
1.创建列表类型数据并给其赋值 >>> aList = [123,'abc',4.56,['inner','list'],7-9j] >>> aList [123, ...
- 在Excel中使用SQL语句查询和筛选
本文转自:http://blog.sina.com.cn/s/blog_5fc375650102e1g5.html 今天在微博上看到@数据分析精选 分享的一篇文章,是关于<在Excel中使用SQ ...
- BASH_SUBSHELL 变量不生效的情况
BASH_SUBSHELL 实现于 Bash 3.0,我一直想不到它在实际编码中有什么用,后来在 Bash 的 Change Log 里找到一句话,才知道它是作调试用的: New variables ...
- [Search Engine] 搜索引擎技术之查询处理
我们之前从开发者的角度谈了一些有关搜索引擎的技术,其实对于用户来说,我们不需要知道网络爬虫到底是怎样爬取网页的,也不需要知道倒排索引是什么,我们只需要输入我们的查询词query,然后能够得到我们想要的 ...
- PHP的大括号(花括号{})使用详解
一.不管什么程序,function name(){}, for(){}, ….这太多了,不说也知道什么用了. 二.$str{4}在字符串的变量的后面跟上{}大括号和中括号[]一样都是把某个字符串变量当 ...
- Unix/Linux进程间通信(二):匿名管道、有名管道 pipe()、mkfifo()
1. 管道概述及相关API应用 1.1 管道相关的关键概念 管道是Linux支持的最初Unix IPC形式之一,具有以下特点: 管道是半双工的,数据只能向一个方向流动:需要双方通信时,需要建立起两个管 ...
- phpcms手机门户相关
相关标签 {$WAP['sitename']}标题 {list_url(3)} 调取栏目链接 {template "wap","header"}{templat ...
- 【HNOI2008】Cards BZOJ 1004
Description 小春现在很清闲,面对书桌上的N张牌,他决定给每张染色,目 前小春只有3种颜色:红色,蓝色,绿色.他询问Sun有多少种染色方案,Sun很快就给出了答案.进一步,小春要求染出Sr张 ...
- Ubuntu16.04 安装MATALAB R2015b教程
1.安装 将镜像文件内文件解压出来,添加执行权限,否则执行 ./install指令会出错 chmod -R 777 MATALAB 执行如下指令 ./install 2.填入补丁内的密匙 在Matla ...
- iOS开发多线程篇 — GCD的常见用法
一.延迟执行 1.介绍 iOS常见的延时执行有2种方式 (1)调用NSObject的方法 [self performSelector:@selector(run) withObject:nil ...