/**
* 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的更多相关文章

  1. [LeetCode] Divide Two Integers 两数相除

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  2. LeetCode: Divide Two Integers 解题报告

    Divide Two Integers Divide two integers without using multiplication, division and mod operator. SOL ...

  3. Leetcode Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...

  4. [LeetCode] Divide Two Integers( bit + 二分法 )

    Divide two integers without using multiplication, division and mod operator. 常常出现大的负数,无法用abs()转换成正数的 ...

  5. Leetcode:Divide Two Integers分析和实现

    题目要求我们用一个32位整数整除另外一个整数,但是不允许我们使用除法,乘法和取模运算. 有趣的问题,下面说一下我的思路: 首先,先给出两个正整数除法运算的过程.假设a为被除数,而b为除数.在计算机中无 ...

  6. leetcode Divide Two Integers python

    class Solution(object): def divide(self, dividend, divisor): """ :type dividend: int ...

  7. leetcode面试准备:Divide Two Integers

    leetcode面试准备:Divide Two Integers 1 题目 Divide two integers without using multiplication, division and ...

  8. [Leetcode][Python]29: Divide Two Integers

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...

  9. 【一天一道LeetCode】#29. Divide Two Integers

    一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...

  10. 【Leetcode】【Medium】Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

随机推荐

  1. 深入理解JVM(五)JVM优化策略

    5.2一些案例: 1.高性能硬件部署策略: (1)背景:某公司升级了硬件(CPU升级为4核,内存增加为16G),发现不定期出现网页失去响应. (2)原因:①内存增加之后,项目中有在内存中处理文件的大对 ...

  2. AX_UserInfo

    UserInfoHelp::userInUserGroup(curuserid(), "Admin")  EmplTable::userId2EmplId(curuserid()) ...

  3. 重写equals和hashCode的方法

    为什么要有 hashCode引用 我们以"HashSet 如何检查重复"为例子来说明为什么要有 hashCode: 当你把对象加入 HashSet 时,HashSet 会先计算对象 ...

  4. [C#]GetFloat提示"指定的转换无效"

    数据库中没有double型,float就表示double值.sql server数据库字段类型与.net的数据类型的对应关系: real(数据库)<--> float(.NET)float ...

  5. Linux 下查看我们的不速之客

    我们通过下面这个命令,可以查看 VPS 上还有谁在登陆: w 输出类似下列信息: 23:20:00 up 960 days, 4:29, 2 user, load average: 0.05, 0.0 ...

  6. java安全与加解密

    1 安全 安全性相关内容分为认证.授权和审计(发现安全问题时可以查看相关历史记录) 用户认证 java API表示主体的是javax.security.auth.Subject类型,表示用户身份标识的 ...

  7. 网页中的数据的4个处理方式:CRUD(Creat, Retrive, Update, Delete)

    网页中的数据的4个处理方式:CRUD(Creat, Retrive, Update, Delete) 2018-12-21, 后续完善

  8. 消息中间件——activeMQ

    Activemq使用教程 解压activmq进入bin\win64 启动activemq.bat 启动成功 浏览器访问http://127.0.0.1:8161 创建maven工程 在pom.xml中 ...

  9. Codeforces gym102152 K.Subarrays OR

    传送:http://codeforces.com/gym/102152/problem/K 题意:给定$n(n\le10^5)$个数$a_i(a_i\le10^9)$,对于任一个子数组中的数进行或操作 ...

  10. 分布式任务调度系统xxl-job源码探究(二、服务中心)

    接下来看下服务端代码 服务端源码 服务端通过管理quartz定时任务组件,分发任务 先从入口看起,由web.xml进入,可以看出,自己编写的代码从applicationcontext-xxl-job- ...