【Leetcode】Divide Two Integers
Divide two integers without using multiplication, division and mod operator.
class Solution {
public:
int divide(int dividend, int divisor) {
long long a = dividend < ? -(long long)dividend : dividend;
long long b = divisor < ? -(long long)divisor : divisor;
bool sign = (dividend >= && divisor >= ) ||
(dividend <= && divisor <= );
long long ans = ;
while (a >= b) {
long long c = b;
for (int i = ; c <= a; c = c << , ++i) {
a -= c;
ans += ( << i);
}
}
return sign ? ans : -ans;
}
};
不能用乘、除和取模,那么还可以用加、减和移位。
符号单独考虑。主要从减法出发,可以通过移位加倍被除数从而实现加速。有一点要特别注意的是可能发生溢出,需要用long long类型来进行中间的运算。
【Leetcode】Divide Two Integers的更多相关文章
- 【leetcode】Divide Two Integers (middle)☆
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 【Leetcode】 - Divide Two Integers 位运算实现整数除法
实现两个整数的除法,不许用乘法.除法和求模.题目被贴上了BinarySearch,但我没理解为什么会和BinarySearch有关系.我想的方法也和BS一点关系都没有. 很早以前我就猜想,整数的乘法是 ...
- 【Leetcode】【Medium】Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 【LeetCode】970. Powerful Integers 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力搜索 日期 题目地址:https://leetc ...
- 【leetcode】970. Powerful Integers
题目如下: Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j fo ...
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
- 【LeetCode】二分 binary_search(共58题)
[4]Median of Two Sorted Arrays [29]Divide Two Integers [33]Search in Rotated Sorted Array [34]Find F ...
- 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)
[LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...
- 【leetcode】1291. Sequential Digits
题目如下: An integer has sequential digits if and only if each digit in the number is one more than the ...
随机推荐
- sql 在存储过程中使用事务(转)
本来想自己写一下,后来发现这个写的比我理解的要好,所以直接拽过来了,链接地址:https://www.cnblogs.com/RascallySnake/archive/2010/05/17/1737 ...
- laravel中的数据库操作(增删改查)方法一
导入命名空间和DBnamespace App\Http\Controllers; use Illuminate\Support\Facades\DB; public function index(){ ...
- 414. Third Maximum Number数组中第三大的数字
[抄题]: Given a non-empty array of integers, return the third maximum number in this array. If it does ...
- orzdba工具安装注意事项
orzdba是一个监控mysql性能的一个比较好用的perl脚本,是淘宝开源的小工具,下载地址http://code.taobao.org/p/orzdba/src/trunk/ 配置过程中除了参照& ...
- 面试题:TCP协议三次握手
一.首先了解TCP报文格式 其中必须了解的字段有: 1.源端口与目的端口:16位,标识出发送端与接收端的端口号. 2.序号:32位,也叫顺序号.seg序号,本报文段所发送的数据的第一个字节的序号,用来 ...
- 在CentOS6.x下安装Compiz——桌面立方体,特效种种
很多人貌似认为compiz必须要emerland,但事实上,没这个必要. compiz+gnome,实用,而又华丽,是个不错的选择. compiz需要显卡驱动,一般情况下不成问题(别忘了这是很新的ce ...
- 登录到 SQL Server 实例
登录到 SQL Server 实例(命令提示符) 登录到 SQL Server 的默认实例 从命令提示符输入以下命令,使用 Windows 身份验证进行连接: sqlcmd [ /E ] ...
- sublime text3安装后html:5+Tab不能快速生成html头部信息的解决办法
sublime text3安装后html:5+Tab不能快速生成html头部信息的解决办法: 需要下载Emmet插件,按网上写的步骤按ctrl+shift+P打开命令面板,输入install,鼠标点击 ...
- cgroup初步分析(1)
cgroup的功能和作用不废话,直说一下cgroup的几条设计准则,有了几条设计准则的约束,就比较容易理解其中的数据结构和函数,至于源代码cgroup.c,无非是两个内容,一是task_struct. ...
- 20169219 SQL注入实验报告
实验介绍 SQL注入技术是利用web应用程序和数据库服务器之间的接口来篡改网站内容的攻击技术.通过把SQL命令插入到Web表单提交框.输入域名框或页面请求框中,最终欺骗服务器执行恶意的SQL命令. 在 ...