-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path0137.只出现一次的数字-ii.java
65 lines (61 loc) · 1.29 KB
/
0137.只出现一次的数字-ii.java
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
/*
* @lc app=leetcode.cn id=137 lang=java
*
* [137] 只出现一次的数字 II
*
* https://leetcode.cn/problems/single-number-ii/description/
*
* algorithms
* Medium (71.75%)
* Likes: 1084
* Dislikes: 0
* Total Accepted: 164.9K
* Total Submissions: 229.6K
* Testcase Example: '[2,2,3,2]'
*
* 给你一个整数数组 nums ,除某个元素仅出现 一次 外,其余每个元素都恰出现 三次 。请你找出并返回那个只出现了一次的元素。
*
* 你必须设计并实现线性时间复杂度的算法且使用常数级空间来解决此问题。
*
*
*
* 示例 1:
*
*
* 输入:nums = [2,2,3,2]
* 输出:3
*
*
* 示例 2:
*
*
* 输入:nums = [0,1,0,1,0,1,99]
* 输出:99
*
*
*
*
* 提示:
*
*
* 1 <= nums.length <= 3 * 10^4
* -2^31 <= nums[i] <= 2^31 - 1
* nums 中,除某个元素仅出现 一次 外,其余每个元素都恰出现 三次
*
*
*/
// @lc code=start
class Solution {
public int singleNumber(int[] nums) {
int ans = 0;
for (int i = 0; i < 32; ++i) {
int total = 0;
for (int num : nums)
total += ((num >> i) & 1);
if (total % 3 != 0)
ans |= (1 << i);
}
return ans;
}
}
// @lc code=end