当前位置:网站首页>[算法] 剑指offer2 golang 面试题4:只出现一次的数字
[算法] 剑指offer2 golang 面试题4:只出现一次的数字
2022-07-06 09:18:00 【邓嘉文Jarvan】
[算法] 剑指offer2 golang 面试题4:只出现一次的数字
题目1:
给你一个整数数组 nums ,除某个元素仅出现 一次 外,其余每个元素都恰出现 三次 。请你找出并返回那个只出现了一次的元素。
示例 1:
输入:nums = [2,2,3,2]
输出:3
示例 2:
输入:nums = [0,1,0,1,0,1,100]
输出:100
提示:
1 <= nums.length <= 3 * 104
-231 <= nums[i] <= 231 - 1
nums 中,除某个元素仅出现 一次 外,其余每个元素都恰出现 三次
进阶:你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/WGki4K
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路1:
//思路: 出现3次,使用 bs []int 来记录 32 个位数的 1 的个数
//如果 bs[i] 的个数是 0 或者 3 的倍数,就让 bs[i] = 0,反之 bs[i] =1
//最后素材 bs => int
代码
func singleNumber(nums []int) int {
//思路: 出现3次,使用 bs []int 来记录 32 个位数的 1 的个数
//如果 bs[i] 的个数是 0 或者 3 的倍数,就让 bs[i] = 0,反之 bs[i] =1
//最后素材 bs => int
//参数处理
if len(nums) == 0 {
return 0
}
//构造 bs
bs := make([]int,32)
//遍历元素给 bs 赋值
for i := 0; i < len(nums); i++ {
for j := 0; j < 32; j++ {
if (nums[i] & (1<<j)) != 0 {
bs[j] ++
}
}
}
//bs[i] 的个数是 0 或者 3 的倍数,就让 bs[i] = 0,反之 bs[i] =1
for j := 0; j < 32; j++ {
if bs[j]%3 != 1 {
bs[j] = 0
}else {
bs[j] = 1
}
}
//格式化 bs 为 res int
//tips: bug golang 64 位机器 int 就是 64 位
var res int32
for j := 0; j < 32; j++ {
if bs[j] == 1 {
res = res | (1 << j)
}
}
return int(res)
}
测试

边栏推荐
- 音乐播放(Toggle && PlayerPrefs)
- (core focus of software engineering review) Chapter V detailed design exercises
- Meanings and differences of PV, UV, IP, VV, CV
- Single chip Bluetooth wireless burning
- (the first set of course design) sub task 1-5 317 (100 points) (dijkstra: heavy edge self loop)
- 1041 Be Unique (20 point(s))(哈希:找第一个出现一次的数)
- 【GNSS】抗差估计(稳健估计)原理及程序实现
- Idea problem record
- FairyGUI人物状态弹窗
- (课设第一套)1-5 317号子任务 (100 分)(Dijkstra:重边自环)
猜你喜欢

Single chip Bluetooth wireless burning

idea中好用的快捷键

第一人称视角的角色移动

FGUI工程打包发布&导入Unity&将UI显示出来的方式

FairyGUI增益BUFF數值改變的顯示

(1) Introduction Guide to R language - the first step of data analysis

C programming exercise

FairyGUI循环列表
![[Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data](/img/28/221b0a51ef5f2e8ed5aeca2de8f463.jpg)
[Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data

There is no red exclamation mark after SVN update
随机推荐
MySQL時間、時區、自動填充0的問題
[Chongqing Guangdong education] Shandong University College Physics reference materials
How to reduce the shutdown time of InnoDB database?
Agile development helps me
Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)
[899] ordered queue
Gateway fails to route according to the service name, and reports an error service unavailable, status=503
InnoDB dirty page refresh mechanism checkpoint in MySQL
RTKLIB: demo5 b34f.1 vs b33
Esp8266 connect onenet (old mqtt mode)
PRIDE-PPPAR源码解析
[offer78] merge multiple ordered linked lists
Remember an experience of ECS being blown up by passwords - closing a small black house, changing passwords, and changing ports
SSD technical features
数据库课程设计:高校教务管理系统(含代码)
编译原理:源程序的预处理及词法分析程序的设计与实现(含代码)
Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance
Itext 7 生成PDF总结
Lock wait timeout exceeded try restarting transaction
C programming exercise