[LeetCode]29. Divide Two Integers两数相除
Given two integers dividend
and divisor
, divide two integers without using multiplication, division and mod operator.
Return the quotient after dividing dividend
by divisor
.
The integer division should truncate toward zero.
Example 1:
Input: dividend = 10, divisor = 3
Output: 3
Example 2:
Input: dividend = 7, divisor = -3
Output: -2
Note:
- Both dividend and divisor will be 32-bit signed integers.
- The divisor will never be 0.
- Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 231 − 1 when the division result overflows.
题目要求不使用乘除和取余的运算得到除法的结果,我们第一个想到的肯定是减法,除法的本质就是减法嘛,但是减法的时间复杂度很不友好,如果被除数是Integer.MAX_VALUE,除数是1,那就很麻烦了
在这个基础上我们想到了更高级一点的操作方法,位移运算,<<1表示把字节左移一位相当于*2,我们不断扩大除数divisor,直到它再扩大于被除数,这个时候我们需要缩小被除数,就用这个被除数减去当前的除数然后再重复之前的过程,就得到了我们想要的结果。这里需要注意的是测试样例存在integer.MAX_VALUE和-1的特殊情况,所以我们要使用long来帮助存储判断
class Solution {
public int divide(int dividend, int divisor) {
long m=Math.abs((long)dividend);
long n=Math.abs((long)divisor);
long res=0,tag=Integer.MAX_VALUE;
if(m<n) return 0;
while(m>=n){
long temp=n,count=1;
while(m>(temp<<1)){
temp<<=1;
count<<=1;
}
m-=temp;
res+=count;
}
if((dividend<0)^(divisor<0)) res=-res;
return (int)(res>tag ? tag:res);
}
}
[LeetCode]29. Divide Two Integers两数相除的更多相关文章
- [LeetCode] 29. Divide Two Integers 两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [leetcode]29. Divide Two Integers两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...
- [leetcode]29. Divide Two Integers 两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 029 Divide Two Integers 两数相除
不使用乘号,除号和取模符号将两数相除.如果溢出返回 MAX_INT.详见:https://leetcode.com/problems/divide-two-integers/description/ ...
- [LintCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- [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(不使用乘除取模,求两数相除) ☆☆☆
转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...
- 【LeetCode每天一题】Divide Two Integers(两整数相除)
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
随机推荐
- UML之用例图详解
原文链接:https://blog.csdn.net/mj_ww/article/details/53020080 UML,即Unified Model Language,统一建模语言.百度百科对他的 ...
- Sample-Code:Bing Search API
Demo link: http://code.msdn.microsoft.com/windowsazure/How-to-use-bing-search-API-4c8b287e Aspx Code ...
- CentOS修改默认yum源为国内yum镜像源
修改CentOS默认yum源为mirrors.163.com 1.首先备份系统自带yum源配置文件/etc/yum.repos.d/CentOS-Base.repomv /etc/yum.repos. ...
- springcloud微服务 总结一
一 什么是微服务 译文: 微服务架构是一种架构模式,它提倡将单一应用程序划分成一组小的服务,服务之间互相协调.互相配合,为用户提供最终价值.每个服务运行在其独立的进程中,服务与服务间采用轻量级的通信机 ...
- python 连接数据库 pymysql模块的使用
一 Python3连接MySQL 本文介绍Python3连接MySQL的第三方库--PyMySQL的基本使用. 1 PyMySQL介绍 PyMySQL 是在 Python3.x 版本中用于连接 MyS ...
- Flask之flask-script 指定端口
简介 Flask-Scropt插件为在Flask里编写额外的脚本提供了支持.这包括运行一个开发服务器,一个定制的Python命令行,用于执行初始化数据库.定时任务和其他属于web应用之外的命令行任务的 ...
- F. Clear the String(区间 DP )//每次都删除一个相同字符的子串 , 最小多少次
https://codeforces.com/contest/1132/problem/F 借鉴:https://www.cnblogs.com/chhokmah/p/10508762.html 题意 ...
- HibernateUtil hibernate4.0以上
package com.test.bbs.util; import org.hibernate.Session; import org.hibernate.SessionFactory; import ...
- Docker_network相关指令
docker network create创建docker网卡docker network create -d overlay --ip-range=192.168.2.0/24 --gateway= ...
- (转)用shell脚本实现杨辉三角的4个实例!
概述: 中国古代数学家在数学的许多重要领域中处于遥遥领先的地位.中国古代数学史曾经有自己光辉灿烂的篇章,而杨辉三角的发现就是十分精彩的一页.杨辉三角形,是二项式系数在三角形中的一种几何排列.杨辉 ...