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: 输 ...
随机推荐
- Less-css扩展指定多层嵌套选择器样式
//扩展Extend Use Method:以在study上扩展指定多层嵌套选择器样式 //Share style .test{ font-size:18px; color:#ffffff; ul{ ...
- Unicode 和 UTF-8 是什么关系?
2015-10-14 10:08 评论: 9 收藏: 4 转载自: http://huoding.com/2015/10/13/472作者: 火丁笔记本文地址:https://linux.cn/ ...
- ES6通过Set数组去重
一.Set 1.定义 Set对象是ES6中新定义的数据结构,类似于数组,它允许你存储任何类型的唯一值,不管是原始值还是对象引用. 2.语法 new Set([iterable]) iterable:可 ...
- Sending 'ccColor4B' (aka 'struct_ccColor4B') to parameter of incompatible type
今天遇到了如下的一个错误, Sending 'ccColor4B' (aka 'struct_ccColor4B') to parameter of incompatible type CiColor ...
- 编译Elasticsearch源码
1.从github上clone es的源码 git clone https://github.com/elastic/elasticsearch.git 2.如果没有安装gradle的话,需要安装g ...
- pop 在列表中和字典中的区别
pop 在列表中和字典中的区别 字典中 pop() 语法:dict.pop(key,[value]) 说明:删除指定键及对应的值,如果在字典中不存在键及value,则返回pop()中指定的key对应的 ...
- kubernetes,Docker网络相关资料链接
1.Why kubernetes not doesn't use libnetwork http://blog.kubernetes.io/2016/01/why-Kubernetes-doesnt- ...
- 我的Android进阶之旅------>Android无第三方Jar包的源代报错:The current class path entry belongs to container ...的解决方法
今天使用第三方Jar包afinal.jar时候.想看一下源码,无法看 然后像加入jar相应的源代码包.也无法加入相应的源代码,报错例如以下:The current class path entry b ...
- kotlin写的几个小例子
Kotlin-AdapterDemo kotlin语言下BaseAdapter,ArrayAdapter,SimpleAdapter,SimpleCursorAdapter四种适配器的示例 工具and ...
- mysql安装前的系统准备工作
一.系统环境总结: