29. Divide Two Integers (JAVA)
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. 注意−231取绝对值后会溢出
- class Solution {
- public int divide(int dividend, int divisor) {
- //handle overflow
- int res = 0;
- if(dividend==Integer.MIN_VALUE)
- {
- if(divisor==-1)
- return Integer.MAX_VALUE;
- res = 1;
- dividend += Math.abs(divisor);
- }
- if(divisor==Integer.MIN_VALUE)
- return res;
- //handle negative
- Boolean isNeg = false;
- if((dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0)) isNeg = true;
- dividend = Math.abs(dividend);
- divisor = Math.abs(divisor);
- int cnt = 0; //记录divisor左移的次数
- //找到商的最高比特位
- while(divisor <= (dividend >> 1)){ //除数与被除数/2相比,为了保证除<被除数,且防止溢出
- divisor <<= 1;
- cnt++;
- }
- //从最高比特位开始求出商的每一位
- while(cnt >= 0){
- if(dividend >= divisor){ //dividend > divisor说明当前比特位为1,否则为0
- dividend -= divisor;
- res += 1 << cnt;
- }
- divisor >>= 1;
- cnt--;
- }
- return isNeg?-res:res;
- }
- }
通过二进制位移完成乘除,每次左移一位,相当于十进制中*2;每次右移一位,相当于十进制中/2
将除数位移至小于被除数的最大值,以获取商的最高位
之后除数每右移一位,求商的后一位。用被除数-除数,如果>=0,说明该二进制位的值位1,反之则为0
29. Divide Two Integers (JAVA)的更多相关文章
- 29. Divide Two Integers - LeetCode
Question 29. Divide Two Integers Solution 题目大意:给定两个数字,求出它们的商,要求不能使用乘法.除法以及求余操作. 思路:说下用移位实现的方法 7/3=2, ...
- [Leetcode][Python]29: Divide Two Integers
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...
- 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, division ...
- 【一天一道LeetCode】#29. Divide Two Integers
一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...
- [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 ...
- 29. Divide Two Integers (INT; Overflow, Bit)
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
随机推荐
- sql 查询结果转百分比
select convert(varchar,convert(decimal(10,2),迟到人次*1.0/在校生人数*100))+'%'
- MYSQL登录函数(第3版本)
已经改进 CREATE DEFINER=`root`@`%` FUNCTION `uc_session_login`( `reqjson` JSON, `srvjson` JSON ) RETURNS ...
- linux下怎么清理缓存
free -m 命令可以查看内存使用情况 sysctl 命令可以临时改变某个系统参数 如:sysctl -w net.ipv4.ip_forward=1 是将forware参数临时改为1 当 ser ...
- mysql error(2003) 10060的再解决
前段时间在window虚拟机上处理过这样的问题 现在在linux上也遇到了这样的问题一项一项的排查 1.网络问题,ping的通 但是telnet (ip) (端口号)失败,telnet(ip)都失败 ...
- ABAP笔记
ABAP程序开发,经常会遇到报表开发需求.使用ABAP的Report类型程序开发报表十分便利,用很少的代码就可以快速开发出一个报表.这种报表需求,抛开各种细枝末节,都可以归结为“三步走”:1.选择屏幕 ...
- Target JRE version (1.7.0_79) does not match project JDK version (java version "1.8.0_171"), will use sources from JDK: 1.7
IDEA不会自动匹配系统的JDK环境.如果在IDEA里面没有配置JDK,那么运行程序时就会报错 之前碰到这个问题卡了一下 顺手记录一下 出现此错误说明IDE中配置的jdk版本和你使用的jdk版本不一致 ...
- 封装poi导出篇
前言 先写的导入,以为导出会很简单,没想到导出的东西也不少,基于常用的几种样式和校验写了一个简单的导出,包括时间,数字,文字长度,下拉框,提示框校验,基础样式包括字体,字体颜色,背景颜色等功能,可以使 ...
- springboot学习二:配置文件配置
springboot默认读取application*.properties #######spring配置####### spring.profiles.active=dev //引入开发配置文件 a ...
- c语言实现:三子棋
问题描述:两个游戏者在3*3棋盘里轮流作标记,如果一个人在行,列或者两个对角线可以作三个标记,则为获胜. 我们首先得打印菜单供玩家选择(可以选择玩游戏或者退出游戏) void menu() { pri ...
- C++builder Tokyo 调用com 不正确的变量类型
C++builder Tokyo 调用com 不正确的变量类型 tt.OleFunction("interface_call","MS01",&erro ...