【LeetCode】029. Divide Two Integers
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
题解:
思路就是被除数减去除数,减尽为止。优化的方法是尽量少的做减法。由于不能用乘法,可以利用位操作,左移一位即为该数乘上2
Solution 1
class Solution {
public:
int divide(int dividend, int divisor) {
if (divisor == || (dividend == INT_MIN && divisor == -)) return INT_MAX;
long long m = abs((long long)dividend);
long long n = abs((long long)divisor); int sign = (dividend < ) ^ (divisor < ) ? - : ;
int res = ;
while (m >= n) {
long long tmp = n, p = ;
while (m >= (tmp << )) {
tmp <<= ;
p <<= ;
}
m -= tmp;
res += p;
}
return sign == ? res : -res;
}
};
Solution 2
class Solution {
public:
int divide(int dividend, int divisor) {
if (!divisor || dividend == INT_MIN && divisor == -)
return INT_MAX;
int sign = ((dividend < ) ^ (divisor < )) ? - : ;
unsigned dvd = abs(dividend);
unsigned dvs = abs(divisor);
int res = ;
while (dvd >= dvs) {
unsigned temp = dvs, multiple = ;
// 不可写作 dvd >= (tmp << 1),因为有可能溢出
while (dvd - temp >= temp) {
temp <<= ;
multiple <<= ;
}
dvd -= temp;
res += multiple;
}
return sign * res;
}
};
【LeetCode】029. Divide Two Integers的更多相关文章
- 【LeetCode】29. Divide Two Integers
题意:不用乘除求余运算,计算除法,溢出返回INT_MAX. 首先考虑边界条件,什么条件下会产生溢出?只有一种情况,即返回值为INT_MAX+1的时候. 不用乘除求余怎么做? 一.利用减法. 耗时太长, ...
- 【一天一道LeetCode】#29. Divide Two Integers
一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...
- 【leetcode】1296. Divide Array in Sets of K Consecutive Numbers
题目如下: Given an array of integers nums and a positive integer k, find whether it's possible to divide ...
- 【LeetCode】Sum of Two Integers
问题描述: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and - ...
- 【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 ...
- 【LeetCode】 454、四数之和 II
题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...
- 【LeetCode】18、四数之和
题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...
- 【LeetCode】15、三数之和为0
题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...
随机推荐
- Eclipse运行错误:Failed to load the JNI shared library的解决办法
出现上述错误的原因是环境变量配置出问题,查看JAVA_HOME这一环境变量的值是否正确. 操作步骤如下, 1.右键“我的电脑”->属性 ↓ 2.打开“高级系统设置”,如下图: ↓ 3.选择“环境 ...
- excel中如何取消自动超链接?
最近做的表格有点多,年终述职也到了.总有一些地方生疏了,幸好还有点小印象.记录下来,以后可以回来看看. 方法一 适合单个链接的取消 1 输入网址后,按回车键确认,快捷键ctrl+z,即可取消,这种不好 ...
- [POI2009]Slw
题目 神题!!只有\(POI\)出得出来的神题!! 只能说好像懂了,不想听蒟蒻废话就右转\(dalao\)的博客 目前网上除官方外仅三篇题解,由于推论无法直观得出且有点复杂,难免不好理解,手玩数据最重 ...
- 表单元素disabled禁用后不能自动提交到服务器
表单元素disabled禁用后不能自动提交到服务器,项目中需要一个元素只展示不修改,所以把一个input元素设置成了disabled="disabled",但是提交的时候改数据值是 ...
- Vue.js学习笔记 第五篇 事件处理
监听事件 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...
- Spring MVC 接收多个实体参数
在SpringMVC 的接收参数中,如果接收一个实体对象,只需要在方法参数中这样做:@RequestBody User user //单个的时候这样接收 @RequestMapping(value = ...
- One 的使用(1)
方法一:使用命令提示符 第一步:打开d盘 C:Users\dcf>d; 第二步:打开工作空间 D:\>Cd workspace 第三步:打开the one D:\workspace& ...
- 如何在java代码中调用一个web项目jsp或者servlet
有时候需要调用一个web项目的jsp或者servlet,但是执行内部的代码,并不是打开jsp,例如需要在一段java代码中清除一个web项目中的缓存,那么可以把清除缓存的代码放在该web项目的一个se ...
- 【P2422】良好的感觉(单调栈优化DP//奇怪的暴力)
话说正解是单调栈优化DP,然而貌似根据某种玄学的推算,这个题暴力出解貌似也是可以的.首先,我们枚举所有的点作为最小点,然后横向展开,遇到更小的就停止...然后再操作一下,看上去时间O(N^2),然而由 ...
- Spring学习之xml配置Bean总结
学习Spring时,我用的是Maven来管理jar包,先看看maven的pom.xml: pom.xml <project xmlns="http://maven.apache.org ...