当前位置:网站首页>Sword finger offer II 001. integer division
Sword finger offer II 001. integer division
2022-07-24 14:42:00 【Learning machine:】
subject :


idea :
Use subtraction instead of division
Pay attention to the border ! Be careful int Value range of !!!
Code :
class Solution {
public int divide(int a, int b) {
boolean sign=true;
int count=0;
// Calculation int Maximum value range minimum value
int max = (int)Math.pow(2,31);
int min = -(int)Math.pow(2,31)-1;
// Judge the boundary
if(a == min && b == -1)
return max;
if(a == min && b == 1)
return min;
if(b==min && a!=min)
return 0;
// If the sign is different, the sign bit is set to negative
if((a > 0 && b < 0) || (a < 0 && b > 0)){
sign = false;
}
// take ab Convert all to positive numbers
a = a>0 ? a : -a;
b = b>0 ? b : -b;
// The negative boundary is taken as " back "
if(a==min){
a=max;
// A special case Still thinking about why ...
if(b==2 || b==4)
count++;
}
if(b==min){
b=max;
}
// Judge again Some use cases can directly jump out
if(a<b)
return 0;
// Use shift to achieve the effect of division
// Maximum shift 31 position
for(int i=31;i>=0;i--){
// Determine whether to subtract
while((a>>i)>=b){
a-=(b<<i);
count+=(1<<i);
}
}
// Set sign bit
if(sign)
return count;
else
return -count;
}
}result :

边栏推荐
- 【NLP】下一站,Embodied AI
- Google Earth Engine——使用MODIS数据进行逐月数据的过火(火灾)面积并导出
- XOR procedure
- The server switches between different CONDA environments and views various user processes
- 对话框管理器第二章:创建框架窗口
- C multithreaded lock collation record
- 使用 Fiddler Hook 报错:502 Fiddler - Connection Failed
- Video game design report template and resources over the years
- Atcoder beginer contest 261e / / bitwise thinking + DP
- Tensorflow framework of deep learning realizes vgg/rnn network / verification code generation and recognition / text classification
猜你喜欢

The server switches between different CONDA environments and views various user processes

Jmmert aggregation test report

Dameng real-time active and standby cluster construction

Number of bytes occupied by variables of type char short int in memory

Leetcode high frequency question 56. merge intervals, merge overlapping intervals into one interval, including all intervals

Maotai ice cream "bucked the trend" and became popular, but its cross-border meaning was not "selling ice cream"

Unity 委托 (Delegate) 的简单理解以及实现

深入浅出边缘云 | 2. 架构

Moving the mouse into select options will trigger the mouseleave event processing scheme

Not configured in app.json (uni releases wechat applet)
随机推荐
Atcoder beginer contest 261 f / / tree array
Activate the newly installed Anaconda in the server
Don't lose heart. The famous research on the explosive influence of Yolo and PageRank has been rejected by the CS summit
Learning rate adjustment strategy in deep learning (1)
LeetCode高频题56. 合并区间,将重叠的区间合并为一个区间,包含所有区间
VS编译后的应用缺少dll
mysql
Is it safe for Huatai Securities to open an account? Can it be handled on the mobile phone?
Kotlin类与继承
Leetcode · daily question · 1184. distance between bus stops · simulation
spark:获取日志中每个时间段的访问量(入门级-简单实现)
A common Dao class and util
SQL Server syntax - create database
2.4. properties of special profile
pip换源
Video game design report template and
spark学习笔记(三)——sparkcore基础知识
Unity 委托 (Delegate) 的简单理解以及实现
Conversion of timestamp and time in Excel
Rasa 3.x learning series -rasa fallbackclassifier source code learning notes