leetcode面试准备:Divide Two Integers

1 题目

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

If it is overflow, return MAX_INT.

接口: public int divide(int dividend, int divisor)

2 思路

题意

不用乘、除、mod 做一个除法运算。

直接用除数去一个一个加,直到被除数被超过的话,会超时。

解决办法:每次将被除数增加1倍,同时将count也增加一倍,如果超过了被除数,那么用被除数减去当前和再继续本操作。

复杂度: O(n)

3 代码

	public int divide(int dividend, int divisor) {
// 处理负数
int xor = dividend ^ divisor;
int signal = xor >= 0 ? 1 : -1;
// 正常逻辑
long count = 0;
long num = Math.abs((long) dividend), div = Math.abs((long) divisor);
long tmp = div;
while (num >= tmp) {
long countTmp = 1;
while (num >= tmp) {
tmp = tmp << 1;
countTmp = countTmp << 1;
}
count += countTmp >> 1;
tmp = tmp >> 1;
num = num - tmp;
tmp = div;
}
// 处理溢出的特殊用例{-2147483648, -1}
long res = count * signal;
return res > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int)res;
}

4 总结

数学思维,考智商。

leetcode面试准备:Divide Two Integers的更多相关文章

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

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

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

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

  3. LeetCode OJ:Divide Two Integers(两数相除)

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

  4. 【LeetCode】029. Divide Two Integers

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

  5. 【LeetCode】29. Divide Two Integers

    题意:不用乘除求余运算,计算除法,溢出返回INT_MAX. 首先考虑边界条件,什么条件下会产生溢出?只有一种情况,即返回值为INT_MAX+1的时候. 不用乘除求余怎么做? 一.利用减法. 耗时太长, ...

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

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

  7. LeetCode: Divide Two Integers 解题报告

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

  8. 【Leetcode】【Medium】Divide Two Integers

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

  9. LeetCode第[29]题(Java):Divide Two Integers

    题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...

随机推荐

  1. 20151213Jquery学习笔记--插件

    插件(Plugin)也成为 jQuery 扩展(Extension),是一种遵循一定规范的应用程序接口编 写出来的程序.目前 jQuery 插件已超过几千种,由来自世界各地的开发者共同编写.验证 和完 ...

  2. cognos 10.2.2 导入samples数据源报错解决

    操作系统:windows 2008R2 X64 数据库:oracle 10.2.0.1 X32 sid:cognosdb86 安装完samples后,执行IBM安装目录webcontent\sampl ...

  3. Masonry约束崩溃

    报错: Trapped uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSLayoutConstraint const ...

  4. iOS开发中常用的分类方法---UIImage+Category

    在开发中使用分类对原有的系统类进行方法扩展,是增强系统原有类功能的常见做法. /** * 自由拉伸一张图片 * * @param name 图片名字 * @param left 左边开始位置比例 值范 ...

  5. Eclipse使用Maven创建Web项目

    一.Maven插件下载.jdk下载 1.maven下载地址: http://maven.apache.org/download.cgi 2.jdk下载地址: http://www.oracle.com ...

  6. What is the Xcopy Command?:

    Quote from: http://pcsupport.about.com/od/commandlinereference/p/xcopy-command.htm The xcopy command ...

  7. zlib压缩解压示例

    #include <stdio.h> #include <string.h> #include <assert.h> #include "zlib.h&q ...

  8. AJAX 简单上手

    AJAX简介 没有AJAX会怎么样?普通的ASP.Net每次执行服务端方法的时候都要刷新当前页面,如实现显示服务器的时间每次都要刷新页面的坏处:页面刷新打断用户操作.速度慢.增加服务器的流量压力.如果 ...

  9. 【搭建开发环境】在 Windows XP 中参与开源项目,搭建 git 和 cygwin 开发环境

    引言 只有一台 Windows XP 家用机,却想在诸如 Git@OSC 之类的开源社区参与开发,本文提供一个入门级的开发环境搭建指引. 涉及工具:Eclipse,EGit,Cygwin. 欢迎来到 ...

  10. android BitmapFacty.Options的用法

    通常我们在开发android应用程序时,在加载图片时常常需要与Bitmap打交道,一般会使用BitmapFactory中提供的相关decode方法获取: 如果一张很大的图片,我们不加处理直接decod ...