当前位置:网站首页>剑指offer day22 位运算(中等)
剑指offer day22 位运算(中等)
2022-04-21 07:00:00 【余cos】
day22题目:剑指 Offer 56 - I. 数组中数字出现的次数、剑指 Offer 56 - II. 数组中数字出现的次数 II
知识点:数组、位运算,难度为中等、中等
学习计划链接:「剑指 Offer」 - 学习计划
| 题目 | 知识点 | 难度 |
|---|---|---|
| 剑指 Offer 56 - I. 数组中数字出现的次数 | 位运算数组 | 中等 |
| 剑指 Offer 56 - II. 数组中数字出现的次数 II | 位运算、数学 | 中等 |
剑指 Offer 56 - I. 数组中数字出现的次数
一个整型数组 nums 里除两个数字之外,其他数字都出现了两次。请写程序找出这两个只出现一次的数字。要求时间复杂度是O(n),空间复杂度是O(1)。
示例 1:
输入: nums = [4,1,4,6]
输出: [1,6] 或 [6,1]
示例 2:
输入: nums = [1,2,10,4,1,4,3,3]
输出: [2,10] 或 [10,2]
限制:
2 <= nums.length <= 10000
思路及代码
分组进行异或,将相同的数字分至一组,不同的两个数组分至不同的一组,通过mask确定分组条件。
/** * @param {number[]} nums * @return {number[]} */
var singleNumbers = function(nums) {
let xor = 0 // 为这两数字的异或
for (let i = 0; i < nums.length; i++)
xor ^= nums[i]
let mask = 1
while((xor & mask) === 0)
mask = mask << 1
let [a, b] = [0, 0]
for (let i = 0; i < nums.length; i++) {
if ((nums[i] & mask) === 0)
a ^= nums[i]
else b ^= nums[i]
}
return [a, b];
};
剑指 Offer 56 - II. 数组中数字出现的次数 II
在一个数组 nums 中除一个数字只出现一次之外,其他数字都出现了三次。请找出那个只出现一次的数字。
示例 1:
输入: nums = [3,4,3,3]
输出: 4
示例 2:
输入: nums = [9,1,7,9,7,9,7]
输出: 1
限制:
1 <= nums.length <= 100001 <= nums[i] < 2^31
思路及代码
每一位贡献加起来%3,为1则为这个数的1。
/** * @param {number[]} nums * @return {number} */
var singleNumber = function(nums) {
let res = new Array(32).fill(0)
for (let i = 0; i < nums.length; ++i) {
let num = nums[i]
for (let j = 0; j < 32 && num; ++j) {
if ((num & 1) === 1)
res[j] += 1
num = num >> 1
}
}
let ans = 0
for (let i = 0; i < 32; ++i) {
if (res[i] % 3 !== 0)
ans += (1 << i)
}
return ans
};
版权声明
本文为[余cos]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_45890533/article/details/124306730
边栏推荐
猜你喜欢

Bluetooth Profile Specification之(AVRCP篇)5.1AVCTP的连接和释放

Actual combat of cloud native kubesphere multi tenant system

Gateway and distributed ID

Leetcode topic -- 120 Minimum length sum of triangles, simple DP

kubesphere3. 0 forgot admin password

2022-04-20: the small regiment goes to participate in the military training. The military training is coming to an end. The officer wants to divide n people in a row into m groups, and then ask each g

结合实际聊聊防反接电路(防反接电路总结)

Bluetooth profile specification (AVRCP chapter) 5.1 connection and release of vctp

MongoDB 实验——数据备份和恢复和数据库优化

2022年R2移动式压力容器充装考试题模拟考试题库及模拟考试
随机推荐
PHP RSA encryption
The interface is not restored after Fiddler changes the font
动态规划--LC474.一和零
Actual combat of cloud native kubesphere multi tenant system
2022-04-20:小团去参加军训,军训快要结束了, 长官想要把大家一排n个人分成m组,然后让每组分别去参加阅兵仪式, 只能选择相邻的人一组,不能随意改变队伍中人的位置, 阅兵仪式上会进行打分,其中
343. Find the product of decomposed integers and maximize integer break
动态规划定点突破 --leetcode题目64.最小路径和
迅为i.MX6Q开发板Openwrt 文件系统构建
Default password when installing Kali for virtual machine (VMX file source on official website)
TypeScript函数泛型
PHP infinite classification (recursive)
Vim插件管理插件Vim-plug
V-has permission control based on jeecgboot
Files and Directories
Flutter 基础组件的使用
Yolov5 5.0调用本地摄像头
Flutter first experience
Laravel packages multiple files and downloads them
C# asp. Net calling Baidu character recognition interface
注解功能补充