Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0f8c08c

Browse filesBrowse files
committed
add: 029
1 parent c248a80 commit 0f8c08c
Copy full SHA for 0f8c08c

File tree

1 file changed

+3
-1
lines changed
Filter options

1 file changed

+3
-1
lines changed

‎note/029/README.md

Copy file name to clipboardExpand all lines: note/029/README.md
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ If it is overflow, return MAX_INT.
1515

1616
首先,我们分析下溢出的情况,也就是当被除数为 `Integer.MIN_VALUE`,除数为 `-1` 时会溢出,因为 `|Integer.MIN_VALUE| = Integer.MAX_VALUE + 1`
1717

18-
然后,我们把除数和被除数都转为 `long` 类型的正数去做下一步操作,我这里以 `22` 和 `3` 相除为例子,因为 `22 >= 3`,我们对 `3` 进行左移一位,也就是乘 2,结果为 `6`,比 `22` 小,我们继续对 `6` 左移一位结果为 `12`,还是比 `22` 小,我们继续对 `12` 左移一位为 `24`,比 `22` 大,这时我们可以分析出,`22` 肯定比 `3` 的 `4` 倍要大,`4` 是怎么来的?因为我们左移了两次,也就是 `1 << 2 = 4`,此时我们记下这个 `4`,然后让 `22 - 3 * 4 = 10`,因为 `10 >= 3`,对 `10` 和 `3` 进行同样的操作,我们可以得到 `2`,此时加上上次的 `4`,和为 `6`,也就是 `22` 比 `3` 的 `6` 倍要大,此时还剩余 `10 - 6 = 4`,因为 `4 >= 3`,所以对 `4` 和 `3` 进行同样的操作,我们发现并不能对 `3` 进行左移了,因为 `4 >= 3`,所以 `1` 倍还是有的,所以加上最后的 `1`,结果为 `6 + 1 = 7`,也就是 `22` 整除 `3` 结果为 `7`,根据以上思路来书写如下代码应该不是难事了吧。
18+
然后,我们把除数和被除数都转为 `long` 类型的正数去做下一步操作,我这里以 `22` 和 `3` 相除为例子,因为 `22 >= 3`,我们对 `3` 进行左移一位,也就是乘 2,结果为 `6`,比 `22` 小,我们继续对 `6` 左移一位结果为 `12`,还是比 `22` 小,我们继续对 `12` 左移一位为 `24`,比 `22` 大,这时我们可以分析出,`22` 肯定比 `3` 的 `4` 倍要大,`4` 是怎么来的?因为我们左移了两次,也就是 `1 << 2 = 4`,此时我们记下这个 `4`,然后让 `22 - 3 * 4 = 10`,因为 `10 >= 3`,对 `10` 和 `3` 进行同样的操作,我们可以得到 `2`,此时加上上次的 `4`,和为 `6`,也就是 `22` 比 `3` 的 `6` 倍要大,此时还剩余 `10 - 6 = 4`,因为 `4 >= 3`,所以对 `4` 和 `3` 进行同样的操作,我们发现并不能对 `3` 进行左移了,因为 `4 >= 3`,所以 `1` 倍还是有的,所以加上最后的 `1`,结果为 `6 + 1 = 7`,也就是 `22` 整除 `3` 结果为 `7`。
19+
20+
最终,我们对结果赋予符号位即可,根据以上思路来书写如下代码应该不是难事了吧。
1921

2022
```java
2123
class Solution {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.