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 ...
随机推荐
- AD中PCB各层的含义
PCB的各层定义及描述: 1. Top Layer(顶层布线层):设计为顶层铜箔走线.如为单面板则没有该层. 2. Bottom Layer(底层布线层):设计为底层铜箔走线. 3. Top/Bott ...
- 9_根轨迹_Part3_分离点/汇合点和根的性质
- WebGL小姐姐教我学画画之起手式
初次接触WebGL,如有错误之处欢迎留言,共同学习进步. v WebGL的自画像 我,WebGL,全名Web Graphics Library,是为了让死宅程序猿们(摊手)能在浏览器上为所欲为的画女朋 ...
- layui文件上传组件“请求上传接口出现异常”问题解决方案
这是一个悲伤的故事,以前开发项目用过很多次这个组件,这次使用了Token,于是报了一些莫名其妙的错误,来复盘一下,警示自己! 刚开始接触layui的同学们肯定经常会看到这个错误 下面我们对这个异常的处 ...
- vue—子组件修改父组件的值
如何在子组件中修改父组件的值第一步:首先得保证父组件中有值吧这是userManage.vue 1 data(){ 2 return{ 3 dialogCreate:'false' 4 } 5 } 第二 ...
- Spring Boot-Profile
文章目录 前言 一.Profile是什么? 二.使用步骤 1.多Profile文件 2.使用yml方式 3.激活方式 总结 前言 不同的环境解释:比如我们开发人员使用开发环境,项目发布时使用生产环境, ...
- centos7 装机后的基本配置
安装完centos7.3后,做一些基本的操作 下面是我的环境的配置,你们可以根据自己的环境搭配相应的配置.修改下就可以了.基本操作一:主机名 centos7有一个新的修改主机名的命令hostnatct ...
- jdk-1.8环境变量配置
1.首先下载好jdk-1.8的安装包. 这个安装也是傻瓜式安装,一直下一步即可.一定要记得中间你所设置的安装路径 2.切记 切记 jdk的安装路径 ! 3.右键"此电脑",点击最下 ...
- B. Build the Permutation
题目分析:我们先简单的分析一下这道题是在干什么啊,给我们三个整数n,a,b,问我们能否构造这样的排列使得序列中有a个极大值,b个极小值,能的话就给出任意一种可能的情况,不能的话就输出-1: 其实一开始 ...
- php实验一专属跳转博文
今天完成了php关于设计个人博客主页的实验一作业. 这是php实验一作业中博客的跳转链接页.