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 ...
随机推荐
- centos 7.0 nginx 1.7.9成功安装过程
centos 7.0根目录 的目录构成 [root@localhost /]# lsbin dev home lib64 mnt proc run srv tmp varboot etc lib me ...
- UEditor百度编辑器,工具栏上自定义添加一个普通按钮
添加一个名叫“hougelou”的普通按钮在工具栏上: 第一步:找到ueditor.config.js文件中的toolbars数组,增加一个“hougelou”字符串,然后找到labelMap数组,对 ...
- 开放数据库互联ODBC配置(odbcconf)
开放数据库互连(ODBC)是微软引进的一种早期数据库接口技术,通过ODBC驱动程序可访问数据库数据:使用ODBC管理器可以完成对数据库的链接操作.笔者利用ODBC接口,将WINDOWS计数器信息写入到 ...
- HDU 1003 maxsum
#include<iostream> #include<vector> using namespace std; typedef struct { int maxsum; in ...
- Flex Builder快捷键
Flex几个最重要的快捷键 代码助手:Ctrl+Space(简体中文操作系统是Alt+/) 快速修正:Ctrl+1 单词补全:Alt+/ 打开外部Java文档:Shift+F2 显示搜索对话框:Ctr ...
- UI第八节——UISegmentedControl
- (void)viewDidLoad { [super viewDidLoad]; NSArray *items = @[@"消息", @"电话" ...
- Unix目录结构的来历
作者: 阮一峰 Unix(包含Linux)的初学者,常常会很困惑,不明白目录结构的含义何在. 举例来说,根目录下面有一个子目录/bin,用于存放二进制程序.但是,/usr子目录下面还有/usr/bin ...
- composer的安装以及laravel框架的安装(一)
laravel号称世界上最好的php框架,没有之一,下面介绍它的安装 laravel学习交流qq群:293798134 composer的安装 : php开发者很多,并且在web开发领域占据绝对统治地 ...
- Java poi读取,写入Excel,处理row和cell可能为空的情况
首先需要导入包 import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.NP ...
- 用Javascript(js)进行HTML转义工具(处理特殊字符显示)
转自:http://blog.csdn.net/hj7jay/article/details/51280405 众所周知页面上的字符内容通常都需要进行HTML转义才能正确显示,尤其对于Input,T ...