Skip to content

Commit 6bfc0b7

Browse files
committed
位操作: 只出现一次的数字
Change-Id: Ifa049b68f356a67913529a6f639e6e3863bfd0c8
1 parent df389a5 commit 6bfc0b7

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @lc app=leetcode.cn id=136 lang=golang
3+
*
4+
* [136] 只出现一次的数字
5+
*
6+
* https://leetcode-cn.com/problems/single-number/description/
7+
*
8+
* algorithms
9+
* Easy (63.24%)
10+
* Likes: 924
11+
* Dislikes: 0
12+
* Total Accepted: 121.3K
13+
* Total Submissions: 188.9K
14+
* Testcase Example: '[2,2,1]'
15+
*
16+
* 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
17+
*
18+
* 说明:
19+
*
20+
* 你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?
21+
*
22+
* 示例 1:
23+
*
24+
* 输入: [2,2,1]
25+
* 输出: 1
26+
*
27+
*
28+
* 示例 2:
29+
*
30+
* 输入: [4,1,2,1,2]
31+
* 输出: 4
32+
*
33+
*/
34+
35+
// @lc code=start
36+
func singleNumber(nums []int) int {
37+
ret := nums[0]
38+
for i := 1; i < len(nums); i++ {
39+
ret ^= nums[i]
40+
}
41+
return ret
42+
}
43+
// @lc code=end
44+

0 commit comments

Comments
 (0)