题目链接:点击打开链接
题目大意:略
解题思路
相关企业
- 字节跳动
AC 代码
- Java
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17class Solution { public int singleNumber(int[] nums) { int[] counts = new int[32]; for(int num : nums) { for(int i = 0; i < 32; i++) { counts[i] += num & 1; // 更新第 i 位 1 的个数之和 num >>= 1; // 第 i 位 --> 第 i 位 } } int res = 0, m = 3; for(int i = 31; i >= 0; i--) { res <<= 1; res |= counts[i] % m; // 恢复第 i 位 } return res; } }
- C++
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18class Solution { public: int singleNumber(vector<int>& nums) { int counts[32] = {0}; // C++ 初始化数组需要写明初始值 0 for(int num : nums) { for(int i = 0; i < 32; i++) { counts[i] += num & 1; // 更新第 i 位 1 的个数之和 num >>= 1; // 第 i 位 --> 第 i 位 } } int res = 0, m = 3; for(int i = 31; i >= 0; i--) { res <<= 1; res |= counts[i] % m; // 恢复第 i 位 } return res; } };
最后
以上就是笑点低枕头最近收集整理的关于LeetCode(剑指 Offer)- 56 - II. 数组中数字出现的次数 II的全部内容,更多相关LeetCode(剑指内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复