File tree Expand file tree Collapse file tree 5 files changed +50
-1
lines changed
src/main/java/com/diguage/algo/leetcode Expand file tree Collapse file tree 5 files changed +50
-1
lines changed Original file line number Diff line number Diff line change @@ -25,11 +25,35 @@ Your algorithm should have a linear runtime complexity. Could you implement it w
25
25
*Output:* 4
26
26
----
27
27
28
+ == 思路分析
28
29
30
+ 只有一个数出现一次,其余数字出现两次,则可以用异或来找出该数。异或两位相异得 1,相同得 0,则出现两次的数字都全部得 0,留下了数字即为只出现一次的数字。
31
+
32
+ image::images/0136-01.png[{image_attr}]
29
33
30
34
[[src-0136]]
35
+ [tabs]
36
+ ====
37
+ 一刷::
38
+ +
39
+ --
31
40
[{java_src_attr}]
32
41
----
33
42
include::{sourcedir}/_0136_SingleNumber.java[tag=answer]
34
43
----
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. 只出现一次的数字 - 位运算,清晰图解^]
35
59
Original file line number Diff line number Diff line change 722
722
|{doc_base_url} /0179-largest-number.adoc[题解]
723
723
|⭕️ 不能直接比较数字的字符串,要比较 `a+b` 和 `b+a` ,这是拼接后的数字。
724
724
725
+ |{counter:codes}
726
+ |{leetcode_base_url} /single-number/[136. Single Number^]
727
+ |{doc_base_url} /0136-single-number.adoc[题解]
728
+ |✅ 位运算,异或
729
+
725
730
|===
726
731
727
732
截止目前,本轮练习一共完成 {codes} 道题。
Original file line number Diff line number Diff line change 31
31
* @since 2020-01-01 12:38
32
32
*/
33
33
public class _0136_SingleNumber {
34
- // tag::answer[]
34
+ // tag::answer[]
35
35
/**
36
36
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Single Number.
37
37
*
38
38
* 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
39
42
*/
40
43
public int singleNumber (int [] nums ) {
41
44
if (Objects .isNull (nums ) || nums .length == 0 ) {
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments