当前位置:网站首页>【剑指 Offer】56 - I. 数组中数字出现的次数
【剑指 Offer】56 - I. 数组中数字出现的次数
2022-07-02 21:32:00 【LuZhouShiLi】
剑指 Offer 56 - I. 数组中数字出现的次数
题目
一个整型数组 nums 里除两个数字之外,其他数字都出现了两次。请写程序找出这两个只出现一次的数字。要求时间复杂度是O(n),空间复杂度是O(1)。
思路
对数组nums进行一遍抑或运算,得到两个只出现一次数字的抑或运算结果,初始化一个数字m = 1,x和y是不同的,所以一定有一位是1,那么只要找到为1的二进制位,就可以将nums拆分成两个子数组,初始化一个辅助变量 m=1 ,通过与运算从右向左循环判断,可获取整数 x⊕y 哪一位是1。然后拆分nums为两个子数组,分别遍历两个子数组执行抑或,就可以得到两个只出现一次的数字。
代码
class Solution {
public:
vector<int> singleNumbers(vector<int>& nums) {
int x = 0, y = 0, n = 0, m = 1;
for(int num : nums)
n ^= num;
while((n & m) == 0)
m <<= 1;
for(int num : nums) {
if(num & m) x ^= num;
else y ^= num;
}
return vector<int> {
x, y};
}
};
边栏推荐
- 7. Build native development environment
- MySQL learning notes (Advanced)
- 26 FPS video super-resolution model DAP! Output 720p Video Online
- Construction and maintenance of business website [3]
- [shutter] statefulwidget component (create statefulwidget component | materialapp component | scaffold component)
- Write the content into the picture with type or echo and view it with WinHex
- One week dynamics of dragon lizard community | 2.07-2.13
- Cloud computing technology [1]
- Volvo's first MPV is exposed! Comfortable and safe, equipped with 2.0T plug-in mixing system, it is worth first-class
- Basic IO interface technology - microcomputer Chapter 7 Notes
猜你喜欢
MySQL learning record (9)
Share the easy-to-use fastadmin open source system - Installation
D4: unpaired image defogging, self enhancement method based on density and depth decomposition (CVPR 2022)
[shutter] statefulwidget component (pageview component)
Add two numbers of leetcode
【零基础一】Navicat下载链接
[shutter] shutter layout component (physicalmodel component)
Report on investment development and strategic recommendations of China's vibration isolator market, 2022-2027
*C语言期末课程设计*——通讯录管理系统(完整项目+源代码+详细注释)
[shutter] statefulwidget component (floatingactionbutton component | refreshindicator component)
随机推荐
Basic knowledge of tree and binary tree (detailed illustration)
China Indonesia advanced wound care market trend report, technological innovation and market forecast
Redis -- three special data types
Share the easy-to-use fastadmin open source system - Installation
System (hierarchical) clustering method and SPSS implementation
[C language] [sword finger offer article] - replace spaces
Research Report on ranking analysis and investment strategic planning of RFID market competitiveness of China's industrial manufacturing 2022-2028 Edition
Analysis of enterprise financial statements [1]
rwctf2022_ QLaaS
Construction and maintenance of business websites [7]
Common authority query instructions in Oracle
Etcd Raft 协议
[shutter] the shutter plug-in is used in the shutter project (shutter plug-in management platform | search shutter plug-in | install shutter plug-in | use shutter plug-in)
Makefile: usage of control functions (error, warning, info)
How is LinkedList added?
treevalue——Master Nested Data Like Tensor
Go cache of go cache series
~91 rotation
Chinese Indian seasoning market trend report, technical dynamic innovation and market forecast
3DES (deSede) encryption CBC mode pkcs7padding filling Base64 encoding key 24byte iv8byte