当前位置:网站首页>[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
边栏推荐
猜你喜欢
[Chongqing Guangdong education] Shandong University College Physics reference materials
[算法] 剑指offer2 golang 面试题10:和为k的子数组
Fairygui character status Popup
FairyGUI摇杆
Prove the time complexity of heap sorting
Mixed use of fairygui button dynamics
[算法] 剑指offer2 golang 面试题2:二进制加法
[Nodejs] 20. Koa2 onion ring model ----- code demonstration
程序设计大作业:教务管理系统(C语言)
地球围绕太阳转
随机推荐
程序设计大作业:教务管理系统(C语言)
FairyGUI循環列錶
[offer78]合并多个有序链表
Special palindromes of daily practice of Blue Bridge Cup
[leetcode15] sum of three numbers
FairyGUI按钮动效的混用
(5) Introduction to R language bioinformatics -- ORF and sequence analysis
[899] ordered queue
音乐播放(Toggle && PlayerPrefs)
(the first set of course design) sub task 1-5 317 (100 points) (dijkstra: heavy edge self loop)
KF UD分解之伪代码实现进阶篇【2】
Solution to the problem of automatic login in Yanshan University Campus Network
How to add music playback function to Arduino project
What are the advantages of using SQL in Excel VBA
單片機藍牙無線燒錄
Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY
rtklib单点定位spp使用抗差估计遇到的问题及解决
FairyGUI复选框与进度条的组合使用
[算法] 劍指offer2 golang 面試題2:二進制加法
C programming exercise