力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台
给你一个整数数组
nums,除某个元素仅出现 一次 外,其余每个元素都恰出现 三次 。请你找出并返回那个只出现了一次的元素。你必须设计并实现线性时间复杂度的算法且使用常数级空间来解决此问题。
解题思路:
力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台
代码如下:
- class Solution {
- public int singleNumber(int[] nums) {
- int res = 0;
- for(int i = 0; i < 32;i++) {
- int total = 0;
- for(int num : nums) {
- total += ((num>>i) & 1);
- }
- if(total % 3 != 0){
- res |= (1 << i);
- }
- }
- return res;
-
- }
- }