/**
* 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. 使用git开发的流程

    1.git常用的主干,分支命令 查看分支 git branch 或者 git branch -v A) 创建分支 git branch Dev_samples_V1.0.0 B) 切换分支 git c ...

  2. 杨其菊201771010134《面向对象程序设计Java》第二周学习总结

    第三章 Java基本程序设计结构 第一部分:(理论知识部分) 本章主要学习:基本内容:数据类型:变量:运算符:类型转换,字符串,输入输出,控制流程,大数值以及数组. 1.基本概念: 1)标识符:由字母 ...

  3. c++11 线程池学习笔记 (一) 任务队列

    学习内容来自一下地址 http://www.cnblogs.com/qicosmos/p/4772486.html github https://github.com/qicosmos/cosmos ...

  4. explain 类型分析

    all 全表扫描 ☆ index 索引全扫描 ☆☆ range 索引范围扫描,常用语<,<=,>=,between等操作 ☆☆☆ ref 使用非唯一索引扫描或唯一索引前缀扫描,返回单 ...

  5. 修改maven 本地仓库,加入阿里云

    阿里云仓库服务 http://maven.aliyun.com/mvn/view maven加入阿里云服务 在maven  conf文件下修改settings.xml 修改本地仓库<localR ...

  6. ABP框架系列之十三:(Authorization-授权)

    Introduction Almost all enterprise applications use authorization in some level. Authorization is us ...

  7. ubuntu 配置jdk报错解决办法

    vi /etc/profile ,添加如下代码 export JAVA_HOME=/home/mark/android/jdk1.8 export JRE_HOME=/home/mark/androi ...

  8. wx:for类表渲染

    列表渲染 wx:for 在组件上使用wx:for控制属性绑定一个数组,即可使用数组中各项的数据重复渲染该组件. 默认数组的当前项的下标变量名默认为index,数组当前项的变量名默认为item < ...

  9. python之路(三)-深浅拷贝

    深浅拷贝用法来自copy模块. 导入模块:import copy 浅拷贝:copy.copy 深拷贝:deepcopy 字面理解:浅拷贝指仅仅拷贝数据集合的第一层数据,深拷贝指拷贝数据集合的所有层.所 ...

  10. oracle utl_http 访问https类型

    https://oracle-base.com/articles/misc/utl_http-and-ssl http://blog.whitehorses.nl/2010/05/27/access- ...