LeetCode 28 Divide Two Integers
Divide two integers without using multiplication, division and mod operator.
思路:1.先将被除数和除数转化为long的非负数,注意一定要为long。由于Integer.MIN_VALUE的绝对值超出了Integer的范围。
2.常理:不论什么正整数num都能够表示为num=2^a+2^b+2^c+...+2^n。故能够採用2^a+2^b+2^c+...+2^n来表示商,即dividend=divisor*(2^a+2^b+2^c+...+2^n),(a,b,c,....m互不相等。且最大为31,最小为0)。
而商的最大值为Integer.MIN_VALUE的绝对值。商最多有32个2的指数次相加。故时间复杂度为常数。
3.divisor*2^a用计算机表示为divisor<<a;
注意:若每次仅仅加一个divisor。则面对Integer.MAX_VALUE除以一个非常小的常数(eg:1。2。3),会超时。
public class Solution {
public int divide(int dividend, int divisor) {
boolean positive = true;
if((dividend>0&&divisor<0)||(dividend<0&&divisor>0))
positive = false;
long did=dividend>=0?(long)dividend:-(long)dividend;
long dis=divisor>=0?(long)divisor:-(long)divisor;
long quotients = positiveDivide(did, dis);
if (!positive)
return (int)-quotients;
return (int)quotients;
}
public long positiveDivide(long did, long dis) {
long[] array = new long[32];
long sum = 0;
int i = 1;
long quotients = 0;
if(dis==1) return did;//为了避免-did=Integer.MIN_VALUE,而dis=1。出现故障
for (array[0]=dis; i < 32 && array[i - 1] <= did; i++)
array[i] = array[i - 1] << 1;
for (i = i - 2; i >= 0; i--) {
if (sum <= did - array[i]) {
sum += array[i];
quotients += 1 << i;
}
}
return quotients;
}
}
优化版,减小内存的消耗。不申请动态数组
public class Solution {
public int divide(int dividend, int divisor) {
boolean positive = true;
if((dividend>0&&divisor<0)||(dividend<0&&divisor>0))
positive = false;
long did=dividend>=0?
(long)dividend:-(long)dividend;
long dis=divisor>=0?(long)divisor:-(long)divisor;
long quotients = positiveDivide(did, dis);
if (!positive)
return (int)-quotients;
return (int)quotients;
}
public long positiveDivide(long did, long dis) {
long sum = 0;
long quotients = 0;
if(dis==1) return did;//为了避免-did=Integer.MIN_VALUE,而dis=1。出现故障
//sum从divisor*2^31的開始加起,不能加则试试加上divisor*2^30。
//若不能则试试divisor*2^29,依此类推
for (int i = 31; i >= 0; i--) {
long temp=dis<<i;//该式为divisor*2^a
//sum<=dividend则说明dividend大于divisor*(2^m+...+2^i),m最大为31
if (sum <= did - temp) {
sum += temp;
quotients += 1 << i;//2^i
}
}
return quotients;
}
}
LeetCode 28 Divide Two Integers的更多相关文章
- [LeetCode] 29. Divide Two Integers 两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- Java for LeetCode 029 Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 【leetcode】Divide Two Integers (middle)☆
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 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】Divide Two Integers
Divide two integers without using multiplication, division and mod operator. class Solution { public ...
随机推荐
- Laravel5.1学习笔记18 数据库4 数据填充
简介 编写数据填充类 使用模型工厂类 调用额外填充类 执行填充 #简介 Laravel includes a simple method of seeding your database with t ...
- java DDD 基于maven开发的探讨
对于DDD我目前的理解是 1.除了数据的基本操作,也可以把一些公用的方法或者类迁移到Infrastructrue 2.对于domain层可以声明各个聚合根的操作接口:例:IXXXRepository ...
- 将子节点的所有父节点ID合并成一个字符串,并更新表
begin for cur_dept in (select SLCATALOG_ID from T_GIS_SLCATALOG) loop UPDATE T_GIS_SLCATALOG SET PAT ...
- 移动web——bootstrap响应式工具
基本介绍 1.利用媒体查询功能并使用这些工具类可以方便的针对不同设备展示或隐藏页面内容. 基本使用 <!DOCTYPE html> <html lang="zh-CN&qu ...
- Tcl之looping
1 While loop while test body The while command evaluates test as an expression. If test is true, the ...
- demo记录
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http ...
- react typescript 子组件调用父组件
//父组件 import * as React from 'react'import { Input } from 'antd'const Search = Input.Searchimport &q ...
- Oracle ASM注意事项
ASM是负载均衡的存储策略,加新磁盘会将其它盘数据平均迁移到新磁盘,删除磁盘会将删除磁盘数据平均写回其它磁盘 1.同一磁盘组如果是在raid上,划分的磁盘越少越好,磁盘组分布在不同raid上性能好: ...
- swift-延时加载函数
//延时加载函数 func delayLoad(){ let time: NSTimeInterval = 2.0 let delay = dispatch_time(DISPATCH_TIME_NO ...
- 文本框、评论框原生js
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...