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. servlet中的ServletConfig对象

    ServletConfig对象对应web.xml文件中的<servlet>节点.当Tomcat初始化一个Servlet时,会创建ServletConfig对象,并将该Servlet的配置信 ...

  2. C语言杂谈

    C语言程序处理过程 预处理:宏定义展开.头文件展开.条件编译,这里并不会检查语法 编译:检查语法,将预处理后文件编译生成汇编文件 汇编:将汇编文件生成目标文件(二进制文件) 链接:将目标文件链接为可执 ...

  3. CSS中宽度与高度

    div的高度 div由的高度是由它里面的字体乘以字体建议的行高确定,跟这个字体大小没有关系(不同字体相同字体大小,会影响div的高度.)如过明确告诉浏览器行高,div高度就是行高. 文字两端对齐: 文 ...

  4. C#编写一个控制台应用程序,输入三角形或者长方形边长,计算其周长和面积并输出

    编写一个控制台应用程序,输入三角形或者长方形边长,计算其周长和面积并输出. 代码: using System; using System.Collections.Generic; using Syst ...

  5. Hadoop 3.1.2报错:xception in thread "main" org.apache.hadoop.fs.UnsupportedFileSystemException: No FileSystem for scheme "hdfs"

    报错内容如下: Exception in thread "main" org.apache.hadoop.fs.UnsupportedFileSystemException: No ...

  6. Shiro 安全框架详解二(概念+权限案例实现)

    Shiro 安全框架详解二 总结内容 一.登录认证 二.Shiro 授权 1. 概念 2. 授权流程图 三.基于 ini 的授权认证案例实现 1. 实现原理图 2. 实现代码 2.1 添加 maven ...

  7. Ubuntu安装开发者平台Backstage

    Ubuntu安装开发者平台Backstage 什么是Backstage? Backstage是一个构建开发者门户的开源平台.通过支持一个集中的软件分类,Backstage可以保存并发布你的微服务和基础 ...

  8. jupyter notebook 调用.py文件

    方法1.利用 %run xx.py 直接运行得出结果. 方法2:利用 %load xx.py 载入代码再点击Run运行,这种方法的好处是可以方便修改代码. 说明: Jupyter Notebook中以 ...

  9. 1.5 万字 + 40 张图解 HTTP 常见面试题

    作者:小林coding 图解计算机基础网站:https://xiaolincoding.com 大家好,我是小林,我最开始写的第一篇图解文章就是这篇: 那时候我也就不到 100 读者,如今这篇阅读都快 ...

  10. 界面优化--如何提升用户体验(Velocity.js和GSAP)

    Velocity.js和GSAP 我们需要提升代码质量来留住用户.作为用户界面的建设者,我们的工作是迅速引导和引导用户的注意力,指导他们如何有效地使用我们的应用程序. 1. 如何提升代码质量 定向聚焦 ...