当前位置:网站首页>[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
2022-07-06 12:51:00 【Deng Jiawen jarvan】
[ Algorithm ] The finger of the sword offer2 golang Interview questions 4: A number that appears only once
subject 1:
Give you an array of integers nums , Except that one element only appears once Outside , Every other element just happens to appear Three times . Please find and return the element that only appears once .
Example 1:
Input :nums = [2,2,3,2]
Output :3
Example 2:
Input :nums = [0,1,0,1,0,1,100]
Output :100
Tips :
1 <= nums.length <= 3 * 104
-231 <= nums[i] <= 231 - 1
nums in , Except that one element only appears once Outside , Every other element just happens to appear Three times
Advanced : Your algorithm should have linear time complexity . Can you do this without using extra space ?
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/WGki4K
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas 1:
// Ideas : appear 3 Time , Use bs []int To record 32 Single digit 1 The number of
// If bs[i] The number of is 0 perhaps 3 Multiple , let bs[i] = 0, conversely bs[i] =1
// Final material bs => int
Code
func singleNumber(nums []int) int {
// Ideas : appear 3 Time , Use bs []int To record 32 Single digit 1 The number of
// If bs[i] The number of is 0 perhaps 3 Multiple , let bs[i] = 0, conversely bs[i] =1
// Final material bs => int
// Processing parameters
if len(nums) == 0 {
return 0
}
// structure bs
bs := make([]int,32)
// Traverse element to bs assignment
for i := 0; i < len(nums); i++ {
for j := 0; j < 32; j++ {
if (nums[i] & (1<<j)) != 0 {
bs[j] ++
}
}
}
//bs[i] The number of is 0 perhaps 3 Multiple , let bs[i] = 0, conversely bs[i] =1
for j := 0; j < 32; j++ {
if bs[j]%3 != 1 {
bs[j] = 0
}else {
bs[j] = 1
}
}
// format bs by res int
//tips: bug golang 64 Bit machine int Namely 64 position
var res int32
for j := 0; j < 32; j++ {
if bs[j] == 1 {
res = res | (1 << j)
}
}
return int(res)
}
test

边栏推荐
- It has been solved by personal practice: MySQL row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT
- Design and implementation of general interface open platform - (39) simple and crude implementation of API services
- Knowledge system of digital IT practitioners | software development methods -- agile
- Fairygui loop list
- Fabrication d'un sac à dos simple fairygui
- Prove the time complexity of heap sorting
- FairyGUI人物状态弹窗
- wsl常用命令
- [offer9] implement queues with two stacks
- FairyGUI增益BUFF數值改變的顯示
猜你喜欢

Latex learning

Unity3d, Alibaba cloud server, platform configuration

Design and implementation of general interface open platform - (39) simple and crude implementation of API services
![[Chongqing Guangdong education] Shandong University College Physics reference materials](/img/56/4ac44729c3e480a4f779d6a890363a.jpg)
[Chongqing Guangdong education] Shandong University College Physics reference materials

Office提示您的许可证不是正版弹框解决

Esp8266 connect onenet (old mqtt mode)

What are the advantages of using SQL in Excel VBA
![[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组](/img/8c/1b6ba3b1830ad28176190170c98628.png)
[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组

idea中导包方法

(5) Introduction to R language bioinformatics -- ORF and sequence analysis
随机推荐
FairyGUI循環列錶
Database table splitting strategy
(core focus of software engineering review) Chapter V detailed design exercises
Unity3d camera, the keyboard controls the front and rear left and right up and down movement, and the mouse controls the rotation, zoom in and out
Compile GDAL source code with nmake (win10, vs2022)
平衡二叉树详解 通俗易懂
Game 280 weekly
[算法] 剑指offer2 golang 面试题7:数组中和为0的3个数字
地球围绕太阳转
idea问题记录
(the first set of course design) 1-4 message passing interface (100 points) (simulation: thread)
(5) Introduction to R language bioinformatics -- ORF and sequence analysis
[算法] 剑指offer2 golang 面试题1:整数除法
抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
Design and implementation of general interface open platform - (39) simple and crude implementation of API services
Office prompts that your license is not genuine pop-up box solution
Excel导入,导出功能实现
Agile development helps me
341. Flatten nested list iterator
How to improve the deletion speed of sequential class containers?