Question

29. Divide Two Integers

Solution

题目大意:给定两个数字,求出它们的商,要求不能使用乘法、除法以及求余操作。

思路:说下用移位实现的方法

7/3=2,7是被除数,3是除数
除数左移,假设移动了n次后,移到最接近被除数,这时被除数=被除数-除数,商的一部分为2^n
如果被除数>除数,则继续循环
除数左移,又移动了m次后,移到最接近被除数,这时被除数=被除数-除数,商的一部分为2^m
最后商为2^n+2^m+...

Java实现:

法1:如果可以用除法,一步就可以了

public int divide2(int dividend, int divisor) {
// overflows
if (dividend == Integer.MIN_VALUE && divisor == -1) return Integer.MAX_VALUE;
// 给定两个数字,求出它们的商,要求不能使用乘法、除法以及求余操作。
return dividend / divisor;
}

法2:下面是用减法实现的,执行超时

public int divide2(int dividend, int divisor) {
// overflows
if (dividend == Integer.MIN_VALUE && divisor == -1) return Integer.MAX_VALUE; int ans = 0;
boolean negative = !((dividend > 0 && divisor > 0) || (dividend < 0 && divisor < 0));
dividend = Math.abs(dividend);
divisor = Math.abs(divisor);
dividend -= divisor;
while (dividend >= 0) {
ans++;
dividend -= divisor;
}
return negative ? -ans : ans;
}

法3:用移位实现

public int divide(int dividend, int divisor) {
// 防止溢出
if (dividend == Integer.MIN_VALUE && divisor == -1) return Integer.MAX_VALUE; // 获取最终结果的符号
int sign = ((dividend < 0) ^ (divisor < 0)) ? -1 : 1;
long dvd = Math.abs((long) dividend);
long dvs = Math.abs((long) divisor);
int ans = 0;
while (dvd >= dvs) {
long tmp = dvs, multiple = 1;
while (dvd >= (tmp << 1)) {
tmp <<= 1;
multiple <<= 1;
}
dvd -= tmp;
ans += multiple;
}
return sign == 1 ? ans : -ans;
}

29. Divide Two Integers - LeetCode的更多相关文章

  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 两数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

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

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

  4. Java [leetcode 29]Divide Two Integers

    题目描述: Divide two integers without using multiplication, division and mod operator. If it is overflow ...

  5. [leetcode]29. Divide Two Integers两整数相除

      Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...

  6. [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆

    转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...

  7. [leetcode]29. Divide Two Integers 两整数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  8. [LeetCode] 29. Divide Two Integers ☆☆

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

  9. [LeetCode]29. Divide Two Integers两数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

随机推荐

  1. c源文件中为什么要包含自己对应的头文件

    另一篇:.c文件和.h文件的关系 引言: 我们经常在c工程中发现,源文件中要包含自己的头文件.一直以来,都不知道为什么这样做.现在,我知道了. 以前的认知: 我认为,.c文件没有必要包含自己的.h文件 ...

  2. C++重载输入流、输出流运算符

    在c++中类的私有成员是不能被直接访问的,需要通过类中提供的成员函数简介的操作这些数据.同时C++ 能够使用流提取运算符 >> 和流插入运算符 << 来输入和输出内置的数据类型 ...

  3. led指示灯电路图大全(八款led指示灯电路设计原理图详解)

    led指示灯电路图大全(八款led指示灯电路设计原理图详解) led指示灯电路图(一) 图1所示电路中只有两个元件,R选用1/6--1/8W碳膜电阻或金属膜电阻,阻值在1--300K之间. Ne为氖泡 ...

  4. 一个模仿微信群聊的H5页面

    开始 上半年小米Max发布的时候,做了一个在朋友圈传播的模仿微信的群聊界面H5页面:一群公司的大咖在群里聊小米Max,用户可以向大咖们提问,以此了解产品. 页面的主体是群聊对话,同时在对话中包含了很多 ...

  5. 什么是实例内部类 Instance inner class有什么语法?

    1.Instance inner class定义,用途和用法 重要语法:马克-to-win:1)实例内部类一定得有个外层类的实例和它绑定在一起,所以可以用This指针.所以必须先实例化外层类之后才能再 ...

  6. idea 配置mapper.xml代码提示

    从代码跳转mapper文件的插件: 在mapper文件中添加dtd约束: 1.下载dtd约束文件 http://mybatis.org/dtd/mybatis-3-config.dtd   http: ...

  7. 牛客网-剑指Offer 二维数组中的查找

    题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...

  8. JavaScript实现按钮改变网页背景色

    运行效果: 源代码: 1 <!DOCTYPE html> 2 <html lang="zh"> 3 <head> 4 <meta char ...

  9. java集合总览

    在编写java程序中,我们最常用的除了八种基本数据类型,String对象外还有一个集合类,在我们的的程序中到处充斥着集合类的身影!java中集合大家族的成员实在是太丰富了,有常用的ArrayList. ...

  10. servlet和action的区别

    1.Servlet:默认在第一次访问时被创建,创建一次,单实例对象 2.Action:访问时被创建,每次访问action的时候,都会创建action对象,创建多次,多实例对象