Divide two numbers,两数相除求商,不能用乘法,除法,取模运算
问题描述:求商,不能用乘法,除法,取模运算。
算法思路:不能用除法,那只能用减法,但是用减法,超时。可以用位移运算,每次除数左移,相当于2倍。
public class DividTwoIntegers {
public int divide(int dividend, int divisor)
{
if(divisor == 0) return Integer.MAX_VALUE;
if(divisor == -1 && dividend == Integer.MIN_VALUE) return Integer.MAX_VALUE;
long pDividend = Math.abs((long)dividend);//取绝对值,放溢出转化为long
long pDivisor = Math.abs((long)divisor);
int result = 0;
while(pDividend >= pDivisor)
{
int count = 0;//记录位移
while(pDividend >= (pDivisor << count))
{
count ++;
}
result += (1 << (count-1));
pDividend -= (pDivisor << (count-1));
}
if((dividend <0 && divisor < 0) || (dividend > 0 && divisor > 0))
{
return result;
}
else
{
return -result;
}
}
public static void main(String[] args)
{
DividTwoIntegers dt = new DividTwoIntegers();
int a = dt.divide(10, 3);
System.out.println(a);
}
}
Divide two numbers,两数相除求商,不能用乘法,除法,取模运算的更多相关文章
- [LeetCode] 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 ...
- 029 Divide Two Integers 两数相除
不使用乘号,除号和取模符号将两数相除.如果溢出返回 MAX_INT.详见:https://leetcode.com/problems/divide-two-integers/description/ ...
- [LintCode] 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 ...
- [Swift]LeetCode29. 两数相除 | Divide Two Integers
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- LeetCode(29): 两数相除
Medium! 题目描述: 给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符. 返回被除数 dividend 除以除数 divisor ...
- python LeetCode 两数相除
近一个月一直在写业务,空闲时间刷刷leetcode,刷题过程中遇到了一道比较有意思的题目,和大家分享. 题目描述: 给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使 ...
- Leetcode 29.两数相除 By Python
给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符. 返回被除数 dividend 除以除数 divisor 得到的商. 示例 1: 输 ...
随机推荐
- Top15的golang学习资源网站
We decided to ask the Go expert and CTO at QArea’s, Maksim Kuharenko, to share his personal list of ...
- IO流入门-第十一章-PrintStream_PrintWriter
DataInputStream和DataOutputStream基本用法和方法示例 /* java.io.PrintStream:标准的输出流,默认打印到控制台,以字节方式 java.io.Print ...
- 模块 - hashlib/subprocess
hashlib 模块 加密算法: hash (152位)散列 哈希 不可逆得 密码背后就是 hash 程序退出hash 值就变了 ,hash值得结果有可能重复 MD5 (128位) 讯息摘要演算法 基 ...
- java 子类不能继承父类的static方法
先来看一段代码 /** * Created by bjchengpeng on 2018/7/19. */ /**运行结果 * woof * woofaa * * woof * Basenjiaa * ...
- c#与lua交互里,错误处理
如果是c#代码出错了 [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))] static int _g_get_down(RealStatePtr ...
- Nordic Blue Tooth
一 . nordic BLE4.0 1.开发nordic的应用需要安装支持keil的pack库和插件 2.nordic的SDK很完整,实例涵盖了几乎所有的应用 https://www.nordicse ...
- python16_day08【异常、多线程】
一.反射及相关 1.isinstance(obj, cls) 检查是否obj是否是类 cls 的对象 class Foo(object): pass obj = Foo() isinstance(ob ...
- Windows换行符问题
MAC 和 Windows的换行符不一样,导致有些情况下,MAC编辑的多行文本,在windows的TXT中只是一行. 使用nodepad++可以正确识别出换行符,而且可以将其转为Windows格式,使 ...
- xpath(待补充)
from lxml import etree html=""" <div> <ul> <li>1</li> <li ...
- 转:oracle物化视图学习笔记
最近学习了一下物化视图,正好经理不在,把学习结果贴出来供大家一起研究一下吧. 先看一下物化视图的大概含义吧,感觉baidu的定义还不错 物化视图,它是用于预先计算并保存表连接或聚集等耗时较多的操作的结 ...