File tree 3 files changed +63
-18
lines changed
src/main/java/com/diguage/algo/leetcode
3 files changed +63
-18
lines changed Original file line number Diff line number Diff line change 1
1
[#0136-single-number]
2
- = 136. Single Number
2
+ = 136. 只出现一次的数字
3
3
4
- { leetcode} /problems/single-number/[LeetCode - Single Number ^]
4
+ https:// leetcode.cn /problems/single-number/[LeetCode - 136. 只出现一次的数字 ^]
5
5
6
- Given a *non-empty* array of integers, every element appears _twice_ except for one. Find that single one.
6
+ 给你一个 *非空* 整数数组 `nums` ,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
7
7
8
- *Note:*
8
+ 你必须设计并实现线性时间复杂度的算法来解决此问题,且该算法只使用常量额外空间。
9
9
10
- Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
10
+ *示例 1 :*
11
11
12
- *Example 1:*
12
+ ....
13
+ 输入:nums = [2,2,1]
14
+ 输出:1
15
+ ....
13
16
14
- [subs="verbatim,quotes,macros"]
15
- ----
16
- *Input:* [2,2,1]
17
- *Output:* 1
18
- ----
17
+ *示例 2 :*
19
18
20
- *Example 2:*
19
+ ....
20
+ 输入:nums = [4,1,2,1,2]
21
+ 输出:4
22
+ ....
23
+
24
+ *示例 3 :*
25
+
26
+ ....
27
+ 输入:nums = [1]
28
+ 输出:1
29
+ ....
30
+
31
+
32
+ *提示:*
33
+
34
+ * `1 \<= nums.length \<= 3 * 10^4^`
35
+ * `+-3 * 10^4^ <= nums[i] <= 3 * 10^4^`
36
+ * 除了某个元素只出现一次以外,其余每个元素均出现两次。
21
37
22
- [subs="verbatim,quotes,macros"]
23
- ----
24
- *Input:* [4,1,2,1,2]
25
- *Output:* 4
26
- ----
27
38
39
+ [#思路分析]
28
40
== 思路分析
29
41
30
- 只有一个数出现一次,其余数字出现两次,则可以用异或来找出该数。异或两位相异得 1 ,相同得 0 ,则出现两次的数字都全部得 0 ,留下了数字即为只出现一次的数字。
42
+ 只有一个数出现一次,其余数字出现两次,则可以用异或来找出该数。异或两位相异得 `1` ,相同得 `0` ,则出现两次的数字都全部得 `0` ,留下了数字即为只出现一次的数字。
31
43
32
44
image::images/0136-01.png[{image_attr}]
33
45
@@ -51,8 +63,18 @@ include::{sourcedir}/_0136_SingleNumber.java[tag=answer]
51
63
include::{sourcedir}/_0136_SingleNumber_2.java[tag=answer]
52
64
----
53
65
--
66
+
67
+ 三刷::
68
+ +
69
+ --
70
+ [{java_src_attr}]
71
+ ----
72
+ include::{sourcedir}/_0136_SingleNumber_3.java[tag=answer]
73
+ ----
74
+ --
54
75
====
55
76
77
+
56
78
== 参考资料
57
79
58
80
. 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. 只出现一次的数字 - 位运算,清晰图解^]
Original file line number Diff line number Diff line change @@ -528,6 +528,11 @@ endif::[]
528
528
|{doc_base_url} /0125-valid-palindrome.adoc[题解]
529
529
|✅ 双指针
530
530
531
+ |{counter:codes2503}
532
+ |{leetcode_base_url} /single-number/[136. Single Number^]
533
+ |{doc_base_url} /0136-single-number.adoc[题解]
534
+ |✅ 位运算。出现两次,则异或后为 `0` ,所有数字异或,最后只剩下出现一次的数字。
535
+
531
536
|===
532
537
533
538
截止目前,本轮练习一共完成 {codes2503} 道题。
Original file line number Diff line number Diff line change
1
+ package com .diguage .algo .leetcode ;
2
+
3
+ public class _0136_SingleNumber_3 {
4
+ // tag::answer[]
5
+
6
+ /**
7
+ * @author D瓜哥 · https://www.diguage.com
8
+ * @since 2025-04-28 08:51:28
9
+ */
10
+ public int singleNumber (int [] nums ) {
11
+ int result = nums [0 ];
12
+ for (int i = 1 ; i < nums .length ; i ++) {
13
+ result ^= nums [i ];
14
+ }
15
+ return result ;
16
+ }
17
+ // end::answer[]
18
+ }
You can’t perform that action at this time.
0 commit comments