Skip to content

Commit b7f45b9

Browse files
committed
二刷136
1 parent b888867 commit b7f45b9

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed

docs/0136-single-number.adoc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,35 @@ Your algorithm should have a linear runtime complexity. Could you implement it w
2525
*Output:* 4
2626
----
2727

28+
== 思路分析
2829

30+
只有一个数出现一次,其余数字出现两次,则可以用异或来找出该数。异或两位相异得 1,相同得 0,则出现两次的数字都全部得 0,留下了数字即为只出现一次的数字。
31+
32+
image::images/0136-01.png[{image_attr}]
2933

3034
[[src-0136]]
35+
[tabs]
36+
====
37+
一刷::
38+
+
39+
--
3140
[{java_src_attr}]
3241
----
3342
include::{sourcedir}/_0136_SingleNumber.java[tag=answer]
3443
----
44+
--
45+
46+
二刷::
47+
+
48+
--
49+
[{java_src_attr}]
50+
----
51+
include::{sourcedir}/_0136_SingleNumber_2.java[tag=answer]
52+
----
53+
--
54+
====
55+
56+
== 参考资料
57+
58+
. https://leetcode.cn/problems/single-number/solutions/2361995/136-zhi-chu-xian-yi-ci-de-shu-zi-wei-yun-iyd0/?envType=study-plan-v2&envId=selected-coding-interview[136. 只出现一次的数字 - 位运算,清晰图解^]
3559

docs/images/0136-01.png

24.8 KB
Loading

logbook/202406.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,11 @@
722722
|{doc_base_url}/0179-largest-number.adoc[题解]
723723
|⭕️ 不能直接比较数字的字符串,要比较 `a+b``b+a`,这是拼接后的数字。
724724

725+
|{counter:codes}
726+
|{leetcode_base_url}/single-number/[136. Single Number^]
727+
|{doc_base_url}/0136-single-number.adoc[题解]
728+
|✅ 位运算,异或
729+
725730
|===
726731

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

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@
3131
* @since 2020-01-01 12:38
3232
*/
3333
public class _0136_SingleNumber {
34-
// tag::answer[]
34+
// tag::answer[]
3535
/**
3636
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Single Number.
3737
*
3838
* Memory Usage: 38.5 MB, less than 98.52% of Java online submissions for Single Number.
39+
*
40+
* @author D瓜哥 · https://www.diguage.com
41+
* @since 2020-01-01 12:38
3942
*/
4043
public int singleNumber(int[] nums) {
4144
if (Objects.isNull(nums) || nums.length == 0) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0136_SingleNumber_2 {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2024-09-19 14:48:40
8+
*/
9+
public int singleNumber(int[] nums) {
10+
int result = 0;
11+
for (int num : nums) {
12+
result ^= num;
13+
}
14+
return result;
15+
}
16+
// end::answer[]
17+
}

0 commit comments

Comments
 (0)