Skip to content

Commit 8ead5e6

Browse files
committed
二刷371
1 parent 3f9131c commit 8ead5e6

File tree

5 files changed

+63
-13
lines changed

5 files changed

+63
-13
lines changed

docs/0371-sum-of-two-integers.adoc

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,6 @@
33

44
{leetcode}/problems/sum-of-two-integers/[LeetCode - Sum of Two Integers^]
55

6-
这道题的关键有几点:
7-
8-
. 通过异或操作获取在不进位的情况下,各位的值;
9-
. 通过相与加移位来来获取各个进位项;
10-
. 重复上面的操作,直到进位项为 0 为止。
11-
12-
思考题:思考如何通过位运算来实现加减乘除?
13-
14-
== 参考资料
15-
16-
. {leetcode}/problems/sum-of-two-integers/discuss/84278/A-summary%3A-how-to-use-bit-manipulation-to-solve-problems-easily-and-efficiently[A summary: how to use bit manipulation to solve problems easily and efficiently - LeetCode Discuss^]
17-
186
Calculate the sum of two integers _a_ and _b_, but you are *not allowed* to use the operator `+` and `-`.
197

208

@@ -35,13 +23,48 @@ Calculate the sum of two integers _a_ and _b_, but you are *not allowed* to use
3523
*Output:* 1
3624
----
3725

26+
== 思路分析
3827

28+
image::images/0371-01.png[{image_attr}]
3929

30+
n = a ⊕ b 非进位和:异或运算
4031

32+
c = a & b << 1 进位:与运算+左移一位
33+
34+
35+
这道题的关键有几点:
36+
37+
. 通过异或操作获取在不进位的情况下,各位的值;
38+
. 通过相与加移位来来获取各个进位项;
39+
. 重复上面的操作,直到进位项为 0 为止。
40+
41+
思考题:思考如何通过位运算来实现加减乘除?
4142

4243
[[src-0371]]
44+
[tabs]
45+
====
46+
一刷::
47+
+
48+
--
4349
[{java_src_attr}]
4450
----
4551
include::{sourcedir}/_0371_SumOfTwoIntegers.java[tag=answer]
4652
----
53+
--
54+
55+
二刷::
56+
+
57+
--
58+
[{java_src_attr}]
59+
----
60+
include::{sourcedir}/_0371_SumOfTwoIntegers_2.java[tag=answer]
61+
----
62+
--
63+
====
64+
65+
66+
67+
== 参考资料
68+
69+
. {leetcode}/problems/sum-of-two-integers/discuss/84278/A-summary%3A-how-to-use-bit-manipulation-to-solve-problems-easily-and-efficiently[A summary: how to use bit manipulation to solve problems easily and efficiently - LeetCode Discuss^]
4770

docs/images/0371-01.png

51 KB
Loading

logbook/202406.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,11 @@
655655
|{doc_base_url}/0011-container-with-most-water.adoc[题解]
656656
|⭕️双指针+贪心
657657

658+
|{counter:codes}
659+
|{leetcode_base_url}/sum-of-two-integers/[371. Sum of Two Integers^]
660+
|{doc_base_url}/0371-sum-of-two-integers.adoc[题解]
661+
|❌位运算
662+
658663
|===
659664

660665
截止目前,本轮练习一共完成 {codes} 道题。

src/main/java/com/diguage/algo/leetcode/_0371_SumOfTwoIntegers.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@
2525
* @since 2020-01-25 17:03
2626
*/
2727
public class _0371_SumOfTwoIntegers {
28-
// tag::answer[]
28+
// tag::answer[]
2929
/**
3030
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Sum of Two Integers.
3131
*
3232
* Memory Usage: 37.9 MB, less than 6.67% of Java online submissions for Sum of Two Integers.
33+
*
34+
* @author D瓜哥 · https://www.diguage.com
35+
* @since 2020-01-25 17:03
3336
*/
3437
public int getSumLoop(int a, int b) {
3538
while (b != 0) {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0371_SumOfTwoIntegers_2 {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2024-09-16 19:56:29
8+
*/
9+
public int getSum(int a, int b) {
10+
// 循环,当进位为 0 时跳出
11+
while (b != 0) {
12+
int c = (a & b) << 1; // c = 进位
13+
a ^= b; // a = 非进位和
14+
b = c; // b = 进位
15+
}
16+
return a;
17+
}
18+
// end::answer[]
19+
}

0 commit comments

Comments
 (0)