[LintCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return 2147483647
Given dividend = 100 and divisor = 9, return 11.
LeetCode上的原题,请参见我之前的博客Divide Two Integers。
解法一:
class Solution {
public:
/**
* @param dividend the dividend
* @param divisor the divisor
* @return the result
*/
int divide(int dividend, int divisor) {
if (divisor == || (dividend == INT_MIN && divisor == -)) return INT_MAX;
long long m = abs((long long)dividend), n = abs((long long)divisor), res = ;
int sign = ((dividend < ) ^ (divisor < )) ? - : ;
if (n == ) return sign == ? m : -m;
while (m >= n) {
long long t = n, p = ;
while (m >= (t << )) {
t <<= ;
p <<= ;
}
res += p;
m -= t;
}
return sign == ? res : -res;
}
};
解法二:
class Solution {
public:
/**
* @param dividend the dividend
* @param divisor the divisor
* @return the result
*/
int divide(int dividend, int divisor) {
long long m = abs((long long)dividend), n = abs((long long)divisor), res = ;
if (m < n) return ;
while (m >= n) {
long long t = n, p = ;
while (m > (t << )) {
t <<= ;
p <<= ;
}
res += p;
m -= t;
}
if ((dividend < ) ^ (divisor < )) res = -res;
return res > INT_MAX ? INT_MAX : res;
}
};
解法三:
class Solution {
public:
/**
* @param dividend the dividend
* @param divisor the divisor
* @return the result
*/
int divide(int dividend, int divisor) {
long long m = abs((long long)dividend), n = abs((long long)divisor), res = ;
if (m < n) return ;
long long t = n, p = ;
while (m > (t << )) {
t <<= ;
p <<= ;
}
res += p + divide(m - t, n);
if ((dividend < ) ^ (divisor < )) res = -res;
return res > INT_MAX ? INT_MAX : res;
}
};
[LintCode] Divide Two Integers 两数相除的更多相关文章
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- [LeetCode] 29. Divide Two Integers 两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- 029 Divide Two Integers 两数相除
不使用乘号,除号和取模符号将两数相除.如果溢出返回 MAX_INT.详见:https://leetcode.com/problems/divide-two-integers/description/ ...
- [LeetCode]29. Divide Two Integers两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [leetcode]29. Divide Two Integers两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...
- 【LeetCode每天一题】Divide Two Integers(两整数相除)
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [leetcode]29. Divide Two Integers 两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [Swift]LeetCode29. 两数相除 | Divide Two Integers
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- LeetCode OJ:Divide Two Integers(两数相除)
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
随机推荐
- 轻松搞定javascript预解析机制(搞定后,一切有关变态面试题都是浮云~~)
hey,guys!我们一起总结一下JS预解析吧! 首先,我们得搞清楚JS预解析和JS逐行执行的关系.其实它们两并不冲突,一个例子轻松理解它们的关系: 你去酒店吃饭,吃饭前你得看下菜谱,点下菜(JS预解 ...
- linux下epoll如何实现高效处理百万句柄的
linux下epoll如何实现高效处理百万句柄的 分类: linux 技术分享 2012-01-06 10:29 4447人阅读 评论(5) 收藏 举报 linuxsocketcachestructl ...
- 如何安装Ecshop for linux
下载 http://update.shopex.com.cn/version/program/ECShop/download_ecshop_utf8.php 解压缩之后把upload文件夹中的内容放到 ...
- OpenGL的消隐与双缓冲
首先是大家可能已经发现,在我们之前提到的所有例子中,在图形的旋转过程中整个图形都有一定程度的闪烁现象,显得图形的过渡极不平滑,这当然不是我们所要的效果,幸好opengl 支 持一个称为双缓存的技术,可 ...
- while do while以及穷举和迭代
今天的新内容1:while循环 格式: while() { } 初始状态要在循环外提前规定 状态改变要写在花括号里面 括号内是循环条件 for循环与while循环的对比: 2:do while 不管循 ...
- 【spring】 <tx:annotation-driven /> 的理解 【转载的】
在使用SpringMvc的时候,配置文件中我们经常看到 annotation-driven 这样的注解,其含义就是支持注解,一般根据前缀 tx.mvc 等也能很直白的理解出来分别的作用.<tx: ...
- ffmpeg处理RTMP流媒体的命令 发送流媒体的命令(UDP,RTP,RTMP)
将文件当做直播送至live ffmpeg -re -i localFile.mp4 -c copy -f flv rtmp://server/live/streamName re限制输出速率,按照 ...
- springMVC 的工作原理和机制(转)
工作原理上面的是springMVC的工作原理图: 1.客户端发出一个http请求给web服务器,web服务器对http请求进行解析,如果匹配DispatcherServlet的请求映射路径(在web. ...
- java&Protocol Buffers
ps: Protocol Buffers简称PB PB 安装配置 下载 PB: 在 PB 官网,下载最新版(或者其他版本)PB,这里为了与 Java 项目中的 PB Maven 依赖版本一致,使用 P ...
- 车销送货上门专用无线开单器-自带PDA无线移动开单系统 与云服务器连接
浩瀚技术配套PDA终端软件 本软件与 数据采集器搭配销售,PDA端软件不单独销售也不含电脑端管理软件 数据采集器 一维扫描头+WIFI+蓝牙+一体打印+PDA软件. 产品特点: 1:通过操作移动手持 ...