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 ...
随机推荐
- CF Gym101933K King's Colors
题目分析 题目要求在树上涂上恰好\(K\)种颜色的方案数. 设\(f(k)\)表示恰好涂上\(k\)种颜色的方案数(答案即为\(f(K)\)). 设\(g(k)\)表示至多涂上\(k\)种颜色的方案数 ...
- 一.在Linux中for和cat遍历文件内容出现no space
以前使用for var in file方式逐行读取文件内容的时候,都没有出现问题,但是今天使用如下代码,会出现“no space” ,目标数据文件内容为6.8M, # 写入临时文件,第一行不能写入 f ...
- 锐捷交换机RG-3760-24 的简单配置与VLAN搭建
要做的事 将交换机和主机连通. 建立vlan,并将主机配置到vlan当中. 连接主机和交换机 安装配置软件 选用SecureCRT 8.0来配置交换机,可在网上下载. 插入配置线 把配置线插入cons ...
- Xcode7解决VVDocumenter 不能使用的方案
Xcode7解决VVDocumenter 不能使用的方案 VVDocumenter-Xcode是Xcode上一款快速添加标准注释,并可以自动生成文档的插件.有了VVDocumenter-Xcode,规 ...
- UITableView控件Protocell的Identifier设置 注意事项
1. 注意:如果想使用Subtitle类型的单元格,需在Storyboard中将Protocell设置为subtitle类型,且Protocell的identifier必须与ViewControll ...
- 单独调用kindeditor的多图上传组件实现多图上传
本例是单独调用kindeditor多图上传的组件来进行多图上传,兼容性你懂得! 官方示例地址:http://kindeditor.net/ke4/examples/multi-image-dialog ...
- performPeriodicTask
/********************************************************************* * @fn performPeriodicTask 执行 ...
- Zookeeper简介和安装(四)
一.简介: Zookeeper是一个分布式协调服务,提供的服务如下: 命名服务:类似于DNS,但仅对于节点 配置管理:服务配置信息的管理 集群管理:Dubbo使用Zookeeper实现服务治理 分布式 ...
- Drbd双机环境安装配置
一.环境准备 1) 操作系统:ubuntu-14.04.1 x64 2) Ubuntu1 192.168.5.179 /dev/sdb1 主节点 Ubuntu2 192.168.5.178 /dev/ ...
- thinkphp 利用GD库在图片上写文字
<?php /** * Created by PhpStorm. * User: Administrator */ namespace Home\Event; use \Think\Image; ...