当前位置:网站首页>LeetCode: 137. 只出现一次的数字 II
LeetCode: 137. 只出现一次的数字 II
2022-06-24 08:08:00 【Whisper_yl】
给你一个整数数组 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 - 1
nums 中,除某个元素仅出现 一次 外,其余每个元素都恰出现 三次
分析:
基础题目为该题:136.只出现一次的数字。基础版采用了一个简单的位运算——异或运算。对于多个0和1,它们异或运算的计算,只与0和1的个数有关,与顺序无关。简而言之也就是符合交换律。所以我们只需要对这n个数各位进行异或运算即可。
而升级版不一样的地方在于只有一个数字出现了一次,其他数字都出现了三次。他们之间的区别在于,出现三次的数字,每一位,要么出现了三次0,要么出现了三次1,出现次数都是三的倍数。于是想到了统计每一位出现的次数,如果不是三的倍数,说明该位属于只出现一次的数字。依据这个思路可以解决该问题,本题重点是对位运算的操作。需要积累一下。
class Solution {
public:
int singleNumber(vector<int>& nums) {
int ans = 0;
for(int i = 0; i < 32; i++){
int total = 0;
for(int num : nums)
total += num >> i & 1; //与1相与,可得第i位结果
if(total % 3 != 0)
ans = ans | (1 << i); //第i位置为1
}
return ans;
}
};
边栏推荐
- Easyexcel single sheet and multi sheet writing
- 支持向量机(SVC,NuSVC,LinearSVC)
- Leetcode -- wrong set
- 2022.06.23 (traversal of lc_144,94145\
- Depens:*** but it is not going to be installed
- 荐书丨《好奇心的秘密》:一个针尖上可以站多少跳舞的小天使?
- L01_一条SQL查询语句是如何执行的?
- Lu Qi: I am most optimistic about these four major technology trends
- Time series data augmentation for deep learning: paper reading of a survey
- Chapter 7 operation bit and bit string (III)
猜你喜欢

June 13-19, 2022 AI industry weekly (issue 102): career development
![[bug] @jsonformat has a problem that the date is less than one day when it is used](/img/09/516799972cd3c18795826199aabc9b.png)
[bug] @jsonformat has a problem that the date is less than one day when it is used

jupyter入门常见的几个坑:

4274. suffix expression

2022.6.13-6.19 AI行业周刊(第102期):职业发展

Numpy NP in numpy c_ And np r_ Explain in detail

L01_ How is an SQL query executed?
![[redis realize Secondary killing Business ①] Overview of Secondary killing Process | Basic Business Realization](/img/a3/9a50e83ece43904a3a622dcdb05b3c.png)
[redis realize Secondary killing Business ①] Overview of Secondary killing Process | Basic Business Realization

Squid代理服务器应用

Transplantation of xuantie e906 -- fanwai 0: Construction of xuantie c906 simulation environment
随机推荐
Webrtc series - network transmission 5: select the optimal connection switching
Leetcode -- linked list
Longest public prefix of leetcode
Weekly recommended short video: is the ultimate form of computing "meta universe"?
L01_一条SQL查询语句是如何执行的?
Time series data augmentation for deep learning: paper reading of a survey
Cdga | how can we do well in data governance?
怎么把mdf和ldf文件导入MySQL workbench中
2021-05-20computed and watch applications and differences
零基础自学SQL课程 | 子查询
MySQL data (Linux Environment) scheduled backup
"I can't understand Sudoku, so I choose to play Sudoku."
最新Windows下Go语言开发环境搭建+GoLand配置
Get post: do you really know the difference between requests??????
Zero foundation self-study SQL course | sub query
[ES6 breakthrough] promise is comparable to native custom encapsulation (10000 words)
Huawei Router: GRE Technology
ApplicationContextInitializer的三种使用方法
[redis realize Secondary killing Business ①] Overview of Secondary killing Process | Basic Business Realization
Implementation process of tcpdump packet capturing