-
Notifications
You must be signed in to change notification settings - Fork 0
/
136.只出现一次的数字.ts
68 lines (66 loc) · 1.17 KB
/
136.只出现一次的数字.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* @lc app=leetcode.cn id=136 lang=typescript
*
* [136] 只出现一次的数字
*
* https://leetcode.cn/problems/single-number/description/
*
* algorithms
* Easy (72.24%)
* Likes: 2847
* Dislikes: 0
* Total Accepted: 895K
* Total Submissions: 1.2M
* Testcase Example: '[2,2,1]'
*
* 给你一个 非空 整数数组 nums ,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
*
* 你必须设计并实现线性时间复杂度的算法来解决此问题,且该算法只使用常量额外空间。
*
*
*
*
*
* 示例 1 :
*
*
* 输入:nums = [2,2,1]
* 输出:1
*
*
* 示例 2 :
*
*
* 输入:nums = [4,1,2,1,2]
* 输出:4
*
*
* 示例 3 :
*
*
* 输入:nums = [1]
* 输出:1
*
*
*
*
* 提示:
*
*
* 1 <= nums.length <= 3 * 10^4
* -3 * 10^4 <= nums[i] <= 3 * 10^4
* 除了某个元素只出现一次以外,其余每个元素均出现两次。
*
*
*
*
*/
// @lc code=start
function singleNumber(nums: number[]): number {
let ans = 0;
for(const num of nums){
ans ^= num;
}
return ans;
};
// @lc code=end