29. Divide Two Integers - LeetCode
Question

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的更多相关文章
- [Leetcode][Python]29: Divide Two Integers
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...
- [LeetCode] 29. Divide Two Integers 两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- 【一天一道LeetCode】#29. Divide Two Integers
一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...
- Java [leetcode 29]Divide Two Integers
题目描述: Divide two integers without using multiplication, division and mod operator. If it is overflow ...
- [leetcode]29. Divide Two Integers两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...
- [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆
转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...
- [leetcode]29. Divide Two Integers 两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [LeetCode] 29. Divide Two Integers ☆☆
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- [LeetCode]29. Divide Two Integers两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
随机推荐
- ES6-11学习笔记--对象的扩展
属性简洁表示法 属性名表达式 Objec.is() 扩展运算符 与 Object.assign() in 对象的遍历方式 属性简洁表示法: 如果属性key跟变量名一样,可简写 let name = ...
- 三种div点击事件
<div id="div" onclick="alert('成功')">点击</div> var oDiv = document.get ...
- java中什么是Interface接口, 请给个实例!
1.Interface接口的定义和用法 先直接上大白话:马克-to-win:接口就是灰常灰常抽象的抽象类,我们可以就像用抽象类一样用接口,只不过,interface抽象到不能再抽象了,以至于里面不能 ...
- java栈stack和堆heap的工作原理,用途及区别?举例说明
java堆和栈的区别[新手可忽略不影响继续学习] Java中内存分成两种:一种是栈stack,一种是堆heap.函数中的一些基本类型的变量(int, float)和对象的引用变量(reference) ...
- Tomcat安装(安装版)
安装Tomcat(安装版) 下载地址https://tomcat.apache.org/ 下载成功,双击进行安装(一路Next). 等待安装结束. 然后打开浏览器输入地址:http://localho ...
- VMware workstation16 许可证
ZF3R0-FHED2-M80TY-8QYGC-NPKYF YF390-0HF8P-M81RQ-2DXQE-M2UT6 ZF71R-DMX85-08DQY-8YMNC-PPHV8 若资金允许,请购买正 ...
- Myeclipse 中怎样更改web项目的访问名
第一步:在要修改的项目名称上右击选择最下面一列的"prepertise"(属性),进入属性设置界面. 第二步:找到左侧菜单栏的"Myeclipse"中的web项 ...
- SpringMVC异常(404,接收参数类型转换错误)
内容 一.异常信息 HTTP Status 400 - type Status report message description The request sent by the client wa ...
- springboot+springsecurity+mybatis plus之用户授权
文章目录 前言 一.导入坐标 二.Users实体类及其数据库表的创建 三.controller,service,mapper层的实现 四.核心--编写配置文件 五.无权限界面和登录界面的实现 前言 即 ...
- Persistent Bits - 题解【二进制】
题面: WhatNext Software creates sequence generators that they hope will produce fairly random sequence ...