当前位置:网站首页>Leetcode/ integer division
Leetcode/ integer division
2022-07-25 05:45:00 【xcrj】
package com.xcrj;
/** * Integer division * Given two integers a and b , Find the quotient of their division a/b , It is required that the multiplication sign... Shall not be used '*'、 devide '/' And the remainder symbol '%' . * The result of integer division should be truncated (truncate) Its decimal part * Suppose our environment can only store 32 Bit signed integer The range of values is [−2^31, 2^31−1]. In this question , If the division result overflows , Then return to 2^31 − 1 * <p> * Microcomputer principle : Original code Inverse code Complement code * The original code is the binary used by people * The internal calculation of the computer is based on the complement * Positive numbers : Original code = Inverse code = Complement code * negative : Original code = The sign bit takes 1, Inverse code = The sign bit takes 1+ Reverse the value bit , Complement code = Inverse code + Last place plus 1= Add the inverse of the original code 1 * <p> * Complement binary representation of signed numbers :byte For example * Integer.toHexString(v) The output is a complement * -128~127 * 0x80->0x7f * 1000,0000-+1->0111,1111 * <p> * java An operator * Will the displacement operator cause the original value of the variable to change * << Move left , Low complement 0. amount to *2 * >> Move right , High complement sign bit . amount to /2 * >>> Move right High compensation 0 * <p> * Operator priority * << >> >>> The operator of has priority over the arithmetic operator ( Including accumulation and subtraction ) All low . Higher priority than comparison operators .+= -= The combination operator has the lowest priority */
public class Solution1 {
/** * The addition operator */
public static int divide(int a, int b) {
int temp = 0;
int quotient = 0;
if (a > 0 && b > 0) {
while (true) {
temp += b;
if (temp <= a) {
quotient++;
} else {
return quotient;
}
}
}
if (a < 0 && b < 0) {
while (true) {
temp += b;
if (temp >= a) {
quotient++;
} else {
return quotient;
}
}
}
if (a > 0 && b < 0) {
while (true) {
a += b;
if (a >= 0) {
quotient--;
} else {
return quotient;
}
}
}
if (a < 0 && b > 0) {
while (true) {
a += b;
if (a <= 0) {
quotient--;
} else {
return quotient;
}
}
}
return quotient;
}
/** * Shift left operator */
public static int divide2(int a, int b) {
// Integer.MIN_VALUE/-1=-Integer.MIN_VALUE Out of range
// Deal with special cases first
if (a == Integer.MIN_VALUE && b == -1) {
return Integer.MAX_VALUE;
}
// Same sign judgment
boolean flag = false;
if ((a < 0 && b < 0) || (a > 0 && b > 0)) {
flag = true;
}
// a, b Take a negative number , Negative numbers represent a larger range than integers 1
long dividend = a > 0 ? -a : a;
long divisor = b > 0 ? -b : b;
// - Divisor >- Divisor , Divisor < Divisor
// Deal with special cases first
if (dividend > divisor) {
return 0;
}
int result = 0;
// !!! Use shift Record the number of shifts required for the divisor to be less than the dividend . Back let 1 Also displacement shift Times to get the result
int shift = 31;
// - Divisor <=- Divisor , Divisor >= Divisor
// When is the divisor less than the dividend
while (dividend <= divisor) {
// Divisor < Divisor , Keep moving left
// How many divisors to weigh 2 Is smaller than the dividend
while (dividend > divisor << shift) {
shift--;
}
// Divisor - The divisor part continues
dividend -= divisor << shift;
// Acquisition quotient
result += 1 << shift;
}
return flag ? result : -result;
}
public static void main(String[] args) {
System.out.println(divide(9, 1));
}
}
边栏推荐
- 2020icpc Jiangxi warm up e.robot sends red packets (DFS)
- After Oracle user a deletes a table under user B's name, can user B recover the deleted table through the recycle bin?
- New discovery of ROS callback function
- The difference between function and task in SystemVerilog
- Mechanism and principle of multihead attention and masked attention
- 新时代生产力工具——FlowUs 息流全方位评测
- HTB-Devel
- Analyzing the principle of DNS resolution in kubernetes cluster
- Introduction summary of using unirx in unity
- 50: Chapter 5: develop admin management service: 3: develop [query whether the admin user name already exists, interface]; (this interface can only be called when logging in; so we have written an int
猜你喜欢

基于ISO13209(OTX)实现EOL下线序列

Arm PWN basic tutorial

The computer accesses the Internet normally with the same network cable, and the mobile phone connects to WiFi successfully, but it cannot access the Internet

ECS is exclusive to old users, and the new purchase of the remaining 10 instances is as low as 3.6% off

Basset: learning the regulatory code of the accessible genome with deep convolutional neural network

求求你别再用 System.currentTimeMillis() 统计代码耗时了,真的太 Low 了!

【每日一练】day(14)

Mechanism and principle of multihead attention and masked attention

Differences and application directions of GPS, base station and IP positioning

Promise implementation
随机推荐
Leetcode 237. delete nodes in the linked list
【每日一练】day(14)
剑指 Offer 05. 替换空格
新时代生产力工具——FlowUs 息流全方位评测
对于von Mises distribution(冯·米塞斯分布)的一点心得
Please stop using system The currenttimemillis() statistical code is time-consuming, which is really too low!
After Oracle user a deletes a table under user B's name, can user B recover the deleted table through the recycle bin?
Summary of common attributes of flex layout
(牛客多校二)J-Link with Arithmetic Progression(最小二乘法/三分)
Leetcode 0121. the best time to buy and sell stocks - simulation from back to front
Xiaomi 12s UTRA Leica watermark generation online tool
C100: smallest hevc visual IOT MCU
Unity accesses chartandgraph chart plug-in
50 places are limited to open | with the news of oceanbase's annual press conference coming!
Leetcode 202. happy number (not happy at all)
基于ISO13209(OTX)实现EOL下线序列
context must be a dict rather解决
剑指 Offer 32 - I. 从上到下打印二叉树
HTB-Optimum
CSDN编程挑战赛之数组编程问题