leetcode — divide-two-integers
/**
* Source : https://oj.leetcode.com/problems/divide-two-integers/
*
* Created by lverpeng on 2017/7/12.
*
* Divide two integers without using multiplication, division and mod operator.
*
* If it is overflow, return MAX_INT.
*/
public class DevideTwoInt {
/**
* 不能使用乘、除、求模
*
* 可以直接每次减去除数,但是效率较低,那可以使用移位,类似乘法,将除数乘以2的倍数,然后再与被除数做减法
*
* @param devident
* @param devisor
* @return
*/
public int devide (int devident, int devisor) {
long[] sub = new long[32];
int newDevident = devident < 0 ? -devident : devident;
int newDevisor = devisor < 0 ? -devisor : devisor;
int count = 0;
sub[count] = newDevisor;
int twiceNum = newDevisor;
while (newDevident > twiceNum) {
twiceNum = newDevisor << count;
sub[count] = twiceNum;
count ++;
}
count --;
int remainder = newDevident;
int result = 0;
while (remainder >= newDevisor) {
if (remainder >= sub[count]) {
remainder -= sub[count];
result = result + (1 << count);
} else {
count --;
}
}
return (devident > 0 ^ devisor > 0) ? -result : result;
}
public static void main(String[] args) {
DevideTwoInt devideTwoInt = new DevideTwoInt();
System.out.println(devideTwoInt.devide(-100, 10));
System.out.println(devideTwoInt.devide(-100, 9));
System.out.println(devideTwoInt.devide(-100, -9));
System.out.println(devideTwoInt.devide(100, 9));
System.out.println(devideTwoInt.devide(0, 9));
}
}
leetcode — divide-two-integers的更多相关文章
- [LeetCode] 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 Divide two integers without using multiplication, division and mod operator. SOL ...
- Leetcode Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...
- [LeetCode] Divide Two Integers( bit + 二分法 )
Divide two integers without using multiplication, division and mod operator. 常常出现大的负数,无法用abs()转换成正数的 ...
- Leetcode:Divide Two Integers分析和实现
题目要求我们用一个32位整数整除另外一个整数,但是不允许我们使用除法,乘法和取模运算. 有趣的问题,下面说一下我的思路: 首先,先给出两个正整数除法运算的过程.假设a为被除数,而b为除数.在计算机中无 ...
- leetcode Divide Two Integers python
class Solution(object): def divide(self, dividend, divisor): """ :type dividend: int ...
- leetcode面试准备:Divide Two Integers
leetcode面试准备:Divide Two Integers 1 题目 Divide two integers without using multiplication, division and ...
- [Leetcode][Python]29: Divide Two Integers
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...
- 【一天一道LeetCode】#29. Divide Two Integers
一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...
- 【Leetcode】【Medium】Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
随机推荐
- Python开发——【条件】语句
单分支 # if 条件: # 满足条件后要执行的代码 if 2>1: print("2>1") 双分支 # if 条件: # 满足条件后要执行的代码 # else: # ...
- 实战Python实现BT种子转化为磁力链接
经常看电影的朋友肯定对BT种子并不陌生,但是BT种子文件相对磁力链来说存储不方便,而且在网站上存放BT文件容易引起版权纠纷,而磁力链相对来说则风险小一些. 将BT种子转换为占用空间更小,分享更方便的磁 ...
- 用ps增加照片的气氛--镜头光晕
1.寻找一张图片 2.新建一个图层填充为黑色 3.选择滤镜---渲染---镜头光晕 4.选择图层模式---滤色. 编辑:千锋UI设计
- db2实现递归调用 机构等树形数据结构形成
WITH n(lev,ID, NAME, PORGID, ORG_ID_TREE) AS (SELECT 0,ID, NAME, PORGID, CAST(ID AS VARCHAR(1024)) F ...
- vc++ openssl 程序签名
RSA一般有两种应用场景: 1.公钥加密.私钥解密:这是数据安全通信领域最常见情形: 2.私钥加验.公钥验签:这主要用于数字签名. 我们这里用到的是第二种情况: 这里是基于OpenSSL,首先 ...
- ABP框架系列之二十五:(Embedded-Resource-Files-嵌入式资源文件)
Introduction ASP.NET Boilerplate provides an easy way of using embedded Razor views (.cshtml files) ...
- 【python-HTMLTestRunner】生成HTMLTestRunner报告报错ERROR 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)
[python-HTMLTestRunner]生成HTMLTestRunner报告报错:ERROR 'ascii' codec can't decode byte 0xe5 in position 0 ...
- [au3]批量输入号码程序
批量输入号码程序 这个文件可以随时产生一个剪贴板文字的文本文件,以供其他程序读取. 这个程序修改了许多次了,主要是针对网络延迟的问题. 最终找到了解决方案:探测输入的界面的反馈信息,也就是反馈的颜色. ...
- MFC 不同窗体之间变量调用
应用场景: (1)主对话框包含一个Tab控件,Tab控件用来切换显示若干子对话框,子对话框类的成员需要互相访问. (2)或者程序中包含多个类,各类之间需要互相访问. 方法1-定义指针成员变量: 详情参 ...
- 读入挂(IO)
快如闪电,清华杜瑜皓的读入挂,一模一样代码,加了这个之后... 细思极恐,and 整整行!!! namespace IO{ #define BUF_SIZE 100000 #define OUT_SI ...