leetcode面试准备:Divide Two Integers
leetcode面试准备:Divide Two Integers
1 题目
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
接口: public int divide(int dividend, int divisor)
2 思路
题意
不用乘、除、mod 做一个除法运算。
解
直接用除数去一个一个加,直到被除数被超过的话,会超时。
解决办法:每次将被除数增加1倍,同时将count也增加一倍,如果超过了被除数,那么用被除数减去当前和再继续本操作。
复杂度: O(n)
3 代码
public int divide(int dividend, int divisor) {
// 处理负数
int xor = dividend ^ divisor;
int signal = xor >= 0 ? 1 : -1;
// 正常逻辑
long count = 0;
long num = Math.abs((long) dividend), div = Math.abs((long) divisor);
long tmp = div;
while (num >= tmp) {
long countTmp = 1;
while (num >= tmp) {
tmp = tmp << 1;
countTmp = countTmp << 1;
}
count += countTmp >> 1;
tmp = tmp >> 1;
num = num - tmp;
tmp = div;
}
// 处理溢出的特殊用例{-2147483648, -1}
long res = count * signal;
return res > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int)res;
}
4 总结
数学思维,考智商。
leetcode面试准备:Divide Two Integers的更多相关文章
- [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 OJ:Divide Two Integers(两数相除)
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 【LeetCode】029. Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 【LeetCode】29. Divide Two Integers
题意:不用乘除求余运算,计算除法,溢出返回INT_MAX. 首先考虑边界条件,什么条件下会产生溢出?只有一种情况,即返回值为INT_MAX+1的时候. 不用乘除求余怎么做? 一.利用减法. 耗时太长, ...
- [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】【Medium】Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- LeetCode第[29]题(Java):Divide Two Integers
题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...
随机推荐
- js高程 第 4章 变量、作用域和内存问题 【笔记】
4.4 小结 JavaScript变量可以用来保存两种类型的值:基本类型值和引用类型值.基本类型的值源自以下 5 种基本数据类型:Undefined.Null.Boolean.Number 和 Str ...
- PV 并发量的计算
http://blog.csdn.net/xingxing513234072/article/details/17336573 PV与并发之间换算的算法换算公式 并发连接数 = PV / 统计时间 * ...
- 20151217jqueryUI学习笔记
工具提示(tooltip),是一个非常实用的 UI.它彻底扩展了 HTML 中的 title 属性,让提示更加丰富,更加可控制,全面提升了用户体验.一. 调用 tooltip()方法在调用 toolt ...
- hdoj1325 Is It A Tree?
Is It A Tree?题目链接 题意: 多组测试数据, 每组数据有多个数对, 表示一条有向边(即第一个数是第二个数的父节点), 以 0,0 为一组测试数据结束标志.当输入-1,-1时测试结束. 从 ...
- 零基础Visual Fox Pro 6.0自学笔记(VFP6.0图文教程)
序:有个哥们读大一,学的金融,由于考试需要去学VFP.拜托我帮忙找教程,发觉网上没有合适的,教学视频多半要收费,优秀文档很少.微软官方也不重视VFP了,真可惜.遂生出写一个入门教程的想法.图文并茂的可 ...
- swift之元组类型
一.元组类型是有N个任意类型的数据组成(N>=0),组成元组类型的数据可以称为“元素” 二.元组的定义 如:let position = (x:10.5,y:20) //两个元素的元组 l ...
- UVA 10066 The Twin Towers(LCS)
Problem B The Twin Towers Input: standard input Output: standard output Once upon a time, in an anci ...
- Windows下管理Python安装包
Pip:A tool for installing and managing Python packages. Pip在Python环境中的地方就相当于Ubuntu环境中的apt-get.以及Mac系 ...
- PHP-traits新特性详解
自 PHP 5.4.0 起,PHP 实现了代码复用的一个方法,称为 traits. Traits 是一种为类似 PHP 的单继承语言而准备的代码复用机制.Trait 为了减少单继承语言的限制,使开发人 ...
- OS概论2
实时系统 实时即表示及时,实时计算可以定义为这样一类计算:系统的正确性,不仅由计算的逻辑结果来确定,而且还取决于产生结果的时间.事实上,实时系统最主要的特征,是将时间作为关键参数,它必须对所接收到的某 ...