列表

详情


NC227. 只出现一次的数字(二)

描述

给定一个整数数组,数组中有一个数出现了一次,其他数出现了三次,请找出只出现了一次的数。

数据范围:数组大小满足 ,数组中每个元素大小满足

示例1

输入:

[1]

输出:

1

示例2

输入:

[1,2,2,2]

输出:

1

原站题解

import java.util.*;
public class Solution {
/**
*
*
*
* @param nums int
* @return int
*/
public int singleNumber (int[] nums) {
// write code here
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

C++ 解法, 执行用时: 7ms, 内存消耗: 1008KB, 提交时间: 2022-01-09

class Solution {
public:
/**
*
*
*
* @param nums intvector
* @return int
*/
int singleNumber(vector<int>& nums) {
// write code here
int arr[32] = {0};
for (auto num : nums) {
for (int i = 0; i < 32; ++i) {
arr[i] += (num >> i) & 1;
}
}
int res = 0;
for (int i = 0 ; i < 32; ++i) {
if (arr[i] % 3 == 1) {
res |= (1 << i);
}
}
return res;
}
};

C++ 解法, 执行用时: 7ms, 内存消耗: 1044KB, 提交时间: 2021-11-29

class Solution {
public:
/**
*
*
*
* @param nums intvector
* @return int
*/
int singleNumber(vector<int>& nums) {
// write code here
sort(nums.begin(),nums.end());
for(int i = 0; i< nums.size(); i = i+3){
if(nums[i] != nums[i+1]) return nums[i];
}
return -1;
}
};

C++ 解法, 执行用时: 7ms, 内存消耗: 1048KB, 提交时间: 2021-12-02

class Solution {
public:
/**
*
*
*
* @param nums intvector
* @return int
*/
int singleNumber(vector<int>& nums) {
int res = 0;
for(int i = 0; i < 32; ++i){
int sum = 0;
for(auto num : nums){
sum += num >> i & 1;
}
if(sum % 3){
res |= 1 << i;
}
}
return res;
}
};

C++ 解法, 执行用时: 7ms, 内存消耗: 1052KB, 提交时间: 2022-01-22

class Solution {
public:
/**
*
*
*
* @param nums intvector
* @return int
*/
int singleNumber(vector<int>& nums) {
// write code here
int point = 1;
int cnt;
int num = 0;
for(int i = 0; i < 32; ++i){
cnt = 0;
for(int j = 0; j < nums.size(); ++j){
if(point & nums[j]){
++cnt;
}
}
if(cnt % 3){
num = num | point;
}
point = point << 1;
}
return num;
}
};

C++ 解法, 执行用时: 7ms, 内存消耗: 1056KB, 提交时间: 2022-06-16

class Solution {
public:
/**
*
*
*
* @param nums intvector
* @return int
*/
int singleNumber(vector<int>& nums) {
// write code here
int res = 0;
for(int i = 0; i < 32; i ++){
int total = 0;
for(auto num : nums){
total += ((num >> i) & 1);
}
if(total % 3){
res |= (1 << i);
}
}
return res;
}
};

上一题