LeetCode: 29. Divide Two Integers (Medium)
1. 原题链接
https://leetcode.com/problems/divide-two-integers/description/
2. 题目要求
给出被除数dividend和除数divisor,求出二者相除的商,余数忽略不计。
注意:不能使用乘法、除法和取余运算
3. 解题思路
陷阱一:MIN_VALUE/-1会溢出。因为Integer.MIN_VALUE = -的绝对值比Integer.MAX_VALUE大1
陷阱二:除数divisor不能等于“0”
思路一:使用一个while循环,当dividend >= divisor时,进入循环。dividend = divident - divisor,每减一次计数器res+1。循环结束后则得到二者之商。
缺点:时间复杂度为O( n ),当被除数很大、除数很小时,效率非常低
public class DivideTwoIntegers29 {
public static void main(String[] args) {
System.out.println(divided(-36, -3));
}
public static int divide(int dividend, int divisor) {
if (divisor == 0 || dividend == Integer.MIN_VALUE && divisor == -1)
return Integer.MAX_VALUE;
int res = 0;
int sign = (dividend < 0) ^ (divisor < 0) ? -1 : 1; // 异或运算,除数和被除数同号为正,异号为负
long dvd = Math.abs((long) dividend);
long dvs = Math.abs((long) divisor);
while (dvd >= dvs) {
dvd -= dvs;
res++;
}
return sign == 1 ? res : -res;
}
}
思路二:采用位移运算,任何一个整数可以表示成以2的幂为底的一组基的线性组合,即num=a_0*2^0+a_1*2^1+a_2*2^2+...+a_n*2^n。基于以上这个公式以及左移一位相当于乘以2,我们先让除数左移直到大于被除数之前得到一个最大的基。然后接下来我们每次尝试减去这个基,如果可以则结果增加加2^k,然后基继续右移迭代,直到基为0为止。因为这个方法的迭代次数是按2的幂直到超过结果,所以时间复杂度为O(logn)。
public class DivideTwoIntegers29 {
public static void main(String[] args) {
System.out.println(divided(-36, -3));
}
public static int divide(int dividend, int divisor) {
if (divisor == 0 || dividend == Integer.MIN_VALUE && divisor == -1)
return Integer.MAX_VALUE;
int res = 0;
int sign = (dividend < 0) ^ (divisor < 0) ? -1 : 1; // 异或运算,除数和被除数同号为正,异号为负
long dvd = Math.abs((long) dividend);
long dvs = Math.abs((long) divisor);
while (dvs <= dvd) {
long temp = dvs, mul = 1;
while (dvd >= temp << 1) { // temp<<1,二进制表示左移一位,等价于temp乘以2
temp = temp << 1;
mul = mul << 1;
System.out.println("temp = " + temp + " " + "mul = " + mul);
}
dvd -= temp;
System.out.println("dvd" + dvd);
res += mul;
}
return sign == 1 ? res : -res;
}
}
LeetCode: 29. Divide Two Integers (Medium)的更多相关文章
- [LeetCode] 29. Divide Two Integers 两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- Java [leetcode 29]Divide Two Integers
题目描述: Divide two integers without using multiplication, division and mod operator. If it is overflow ...
- [leetcode]29. Divide Two Integers两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...
- [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆
转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...
- [leetcode]29. Divide Two Integers 两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [LeetCode] 29. 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 ...
- LeetCode 29 Divide Two Integers (不使用乘法,除法,求模计算两个数的除法)
题目链接: https://leetcode.com/problems/divide-two-integers/?tab=Description Problem :不使用乘法,除法,求模计算两个数 ...
- [leetcode] 29. divide two integers
这道题目一直不会做,因为要考虑的corner case 太多. 1. divisor equals 0. 2. dividend equals 0. 3. Is the result negative ...
随机推荐
- websphere 配置库中已存在应用程序,异常处理
from:http://mengdboy.iteye.com/blog/1677379 出现此问题的原因之一:操作界面上没有卸载完成. 进行一下操作: 1.删除 $WAS_HOME/profiles/ ...
- css 17课--
---------------------------------------------------------------------------------------------------- ...
- Mac iTerm2使用总结
1.快捷键大全新建标签:command + t关闭标签:command + w切换标签:command + 数字 command + 左右方向键切换全屏:command + enter查找:comma ...
- [19/04/01-星期一] IO技术_字节流分类总结(含字节数组(Array)流、字节数据(Data)流、字节对象(Object)流)
一.字节流分类概括 -->1.ByteArrayInputStream /ByteArrayOutputStream(数组字节输入输出) InputStream/OutputStr ...
- 第六章.MyBatis缓存结构
一级缓存 测试案例: MyBatisTest.java //缓存 @Test public void testFindCustomerCache1() throws Exception{ SqlSes ...
- windows自定义快捷键功能
如下:
- 全局ajax的使用
一.ajax介绍 详细介绍请看:http://www.runoob.com/ajax/ajax-tutorial.html AJAX = Asynchronous JavaScript and XML ...
- 【转】理解Callable 和 Spring DeferredResult
http://www.cnblogs.com/aheizi/p/5659030.html 1-介绍 Servlet 3中的异步支持为在另一个线程中处理HTTP请求提供了可能性.当有一个长时间运行的任务 ...
- 二十八、详述 IntelliJ IDEA 远程调试 Tomcat 的方法
在调试代码的过程中,为了更好的定位及解决问题,有时候需要我们使用远程调试的方法.在本文中,就让我们一起来看看,如何利用 IntelliJ IDEA 进行远程 Tomcat 的调试. 首先,配置remo ...
- python drf+xadmin+react+dva+react-native+sentry+nginx 搭建前后端分离的博客完整平台
前言: 经过差不多半年的开发,搭建从前端到服务器,实现了前后端分离的一个集PC端.移动端的多端应用,实属不易,今天得空,好好写篇文章,记录这些天的成果.同时也做个分享. 演示网站地址: http:// ...