当前位置:网站首页>LeetCode 0137. 只出现一次的数字 II
LeetCode 0137. 只出现一次的数字 II
2022-07-26 17:31:00 【Tisfy】
【LetMeFly】137.只出现一次的数字 II
力扣题目链接:https://leetcode.cn/problems/single-number-ii/
给你一个整数数组 nums ,除某个元素仅出现 一次 外,其余每个元素都恰出现 三次 。请你找出并返回那个只出现了一次的元素。
示例 1:
输入:nums = [2,2,3,2] 输出:3
示例 2:
输入:nums = [0,1,0,1,0,1,99] 输出:99
提示:
1 <= nums.length <= 3 * 104-231 <= nums[i] <= 231 - 1nums中,除某个元素仅出现 一次 外,其余每个元素都恰出现 三次
进阶:你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?
方法一:哈希表
这道题使用哈希表最简单,只是复杂度不符合进阶要求。
对于C++,直接使用unordered_map统计一下每个数字出现的次数,然后遍历一遍哈希表,返回只出现了一次的数字即可。
- 时间复杂度 O ( n ) O(n) O(n),其中 n n n是数组中数字的个数
- 空间复杂度 O ( n ) O(n) O(n),不符合本题进阶的 O ( 1 ) O(1) O(1)要求
AC代码
C++
class Solution {
public:
int singleNumber(vector<int>& nums) {
unordered_map<int, int> ma;
for (int& t : nums) {
ma[t]++;
}
for (auto& [num, times] : ma) {
if (times == 1)
return num;
}
return -1; // FAKE_RETURN
}
};
方法二:位运算
稍微多想一想,这道题可以使用位运算来解决。
一个 i n t int int类型的数字有 32 32 32位,对于其中的某一位:
遍历所有的数字,并统计这一位出现的次数。
出现了 3 3 3次的数字,对这一位的出现次数的贡献要么是 3 3 3,要么是 0 0 0。总之都是 3 3 3的倍数。
如果只出现了一次的数字的这一位是 1 1 1,那么这一位出现的总次数就是 3 的倍数 + 1 3的倍数+1 3的倍数+1;
如果只出现了一次的数字的这一位是 0 0 0,那么这一位出现的总次数就是 3 的倍数 3的倍数 3的倍数。
因此,对于某一位,如果出现次数不是 3 3 3的倍数,就让答案的这一位为 1 1 1,最终即能得到只出现了一次的数字
- 时间复杂度 O ( n log C ) O(n\log C) O(nlogC),其中 n n n是数组中数字的个数, C C C是一个数字的范围( 2 32 2^{32} 232)。因此 log C \log C logC就是 32 32 32
- 空间复杂度 O ( 1 ) O(1) O(1),符合本题进阶的 O ( 1 ) O(1) O(1)要求
AC代码
C++
class Solution {
public:
int singleNumber(vector<int>& nums) {
int ans = 0;
for (int i = 0; i < 32; i++) {
int times = 0;
for (int& t : nums) {
times += (t >> i) & 1;
}
if (times % 3) {
ans |= (1 << i);
}
}
return ans;
}
};
其实还有方法三:数字电路设计。太妙了,感兴趣了可参考官方题解
class Solution {
public:
int singleNumber(vector<int>& nums) {
int a = 0, b = 0;
for (int num: nums) {
b = ~a & (b ^ num);
a = ~b & (a ^ num);
}
return b;
}
};
同步发文于CSDN,原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/125991647
边栏推荐
- 7月30号PMP考试延期后我们应该做什么?
- [static code quality analysis tool] Shanghai daoning brings you sonarource/sonarqube download, trial and tutorial
- Spark统一内存划分
- AI sky covering DL multilayer perceptron
- PMP考试详解,新考纲有什么变化?
- 2022河南萌新联赛第(三)场:河南大学
- Simple uploading and downloading of Web project files
- 【集训Day2】cinema ticket
- [training Day2] sculpture
- 来吧开发者!不只为了 20 万奖金,试试用最好的“积木”来一场头脑风暴吧!
猜你喜欢

推荐效果不如意,不如试试飞桨图学习

国际大咖 VS 本土开源新星 | ApacheCon Asia 主题演讲议程全览

JS closure simulates private variable interview questions and immediately executes function Iife

JS function scope variables declare that the variables that promote the scope chain without VaR are global variables

跨站脚本攻击(XSS)

深度学习实验:Softmax实现手写数字识别

带你熟悉云网络的“电话簿”:DNS

2、 Topic communication principle, code implementation

线性回归——以一道等差数列的题为例

SQL中去去重的三种方式
随机推荐
1、 Header file, output format,::, namespace
有一说一,阿里P7的薪资待遇是真的香
【云原生】 iVX 低代码开发 引入腾讯地图并在线预览
Heavy! The 2022 China open source development blue book was officially released
Quartz trigger rule
kaggle猫狗数据集开源——用于经典CNN分类实战
College personnel management system based on jsp+servlet
[cloud native] IVX low code development was introduced into Tencent map and previewed online
202. Happy number
URL jump vulnerability
JS closure simulates private variable interview questions and immediately executes function Iife
长征证券开户安全吗?
Overview of the agenda of the keynote speech of apachecon Asia, an international celebrity vs a local open source star
1、 C language program structure, compilation and operation, data type related
Come on developer! Not only for the 200000 bonus, try the best "building blocks" for a brainstorming!
Several ways to resolve hash conflicts
Cross Site Request Forgery (CSRF)
How to assemble a registry?
8.1 Diffie-Hellman密钥交换
Analysis of interface testing