/**
* 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. flask 未完待续

    Flask - 一个短小精悍.可扩展的一个Web框架很多可用的第三方组件:http://flask.pocoo.org/extensions/blogs:https://www.cnblogs.com ...

  2. Python中的urllib

    urllib提供了一系列URL的功能. Get urllib的request模块可以非常方便的抓取URL内容,也就是发送一个GET请求到指定的页面,然后返回HTTP响应: 例如,对豆瓣的一个URLht ...

  3. downLoad

    String root= ServletActionContext.getServletContext().getRealPath(File.separator).replace("\\&q ...

  4. finereport 下拉复选框多选

  5. C#sealed和final

    sealed用于类时,该类被称为密封类,密封类不能被继承: C#提出了密封方法(sealed method) 的概念,以防止在方法所在类的派生类中对该方法的重载.对方法可以使用sealed 修饰符,这 ...

  6. SpringMVC+JQuery实现头像编辑器

    一.简单说明 本头像编辑器主要实现了图片的上传.显示(不溢出父窗口).旋转.裁剪功能! 图片的上传用到的是异步上传,页面不进行刷新,原理是通过JQuery的异步提交+SpringMVC的上传 上传完毕 ...

  7. 20154305 齐帅 PC平台逆向破解

    Exp1 PC平台逆向破解 一.实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时 ...

  8. Maths | 为什么点积等价于投影后的乘积

    目录 1. 复习点积 2. 点积的对称性 3. 矩阵与变换的关系 4. 一维矩阵也是一种线性变换 5. 最终解释:为什么是投影 先上结论: \(\boldsymbol v\)和\(\boldsymbo ...

  9. python3调用zabbix api

    前言 今天写了一个zabbix api,这里整理一下,过程中自己也学习了一些东西. 需求 我们需要查一个文件内容如下: cat /ops/flume_hosts node1 node2 需要把这个文件 ...

  10. 第二周Access课总结

    一.问;这节课你学到了什么知识? 答:回忆上周主要学了关于Access的基础知识和基本操作,一转眼,这周也学到了很多,主要学Access的数据类型的表的建立和管理相关的操作! 收获多少在于学了多少,正 ...